Widget Kit and UserDefaults

Hi,
I keep getting the error “Type ‘()’ cannot conform to ‘View’; only struct/enum/class types can conform to protocols” on lines 99 and 100, here is the code:

import SwiftUI
import Intents
import UserNotifications
class Riddles {
    static var text = String()
    static var isWednesday = Bool()
    static func largeWidget() {
        Riddles.checkWeekday()
        let userDefaults = UserDefaults(suiteName: "group.SetRiddle")
        if Riddles.isWednesday == false {
            if userDefaults?.value(forKey: "currentRiddle") != nil || userDefaults?.value(forKey: "previousRiddle") != nil {
                Riddles.text = "\(UserDefaults(suiteName: "group.SetRiddle")?.value(forKey: "currentRiddle") as! String) \n\n\(UserDefaults(suiteName: "group.SetRiddle")?.value(forKey: "previousRiddle") as! String)"
            }else{
                Riddles.text = "Please open app to display riddle"
            }
        }else {
            Riddles.text = "Please open app to display riddle"
        }
    }
    static func mediumWidget() {
        Riddles.checkWeekday()
        let userDefaults = UserDefaults(suiteName: "group.SetRiddle")
        if Riddles.isWednesday == true {
            if userDefaults?.value(forKey: "currentRiddle") != nil {
                Riddles.text = UserDefaults(suiteName: "group.SetRiddle")?.value(forKey: "currentRiddle") as! String
            }else{
                Riddles.text = "Please open app to display riddle"
            }
        }else {
            Riddles.text = "Please open app to display riddle"
        }
    }
    static func checkWeekday() {
        let today = Date()
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar.dateComponents([.weekday], from: today)
        
        if components.weekday == 4 {
            Riddles.isWednesday = true
        }else {
            Riddles.isWednesday = false
        }
        
    }
}
struct Provider: IntentTimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), text: "", configuration: ConfigurationIntent())
    }
    func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), text: "", configuration: configuration)
        completion(entry)
    }
    func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        var entries: [SimpleEntry] = []
        
        // Generate a timeline consisting of five entries an hour apart, starting from the current date.
        let currentDate = Date()
        for hourOffset in 0 ..< 5 {
            let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
            let entry = SimpleEntry(date: entryDate, text: "", configuration: configuration)
            entries.append(entry)
        }
        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }
}
struct SimpleEntry: TimelineEntry {
    let date: Date
    let text: String
    let configuration: ConfigurationIntent
}
struct RiddleWidgetEntryView : View {
    var entry: Provider.Entry
    
    @Environment(\.widgetFamily) var family: WidgetFamily
    
    @ViewBuilder
    var body: some View {
        
        switch family {
        case .systemMedium: Riddles.mediumWidget()
        case .systemLarge: Riddles.largeWidget()
        }
        ZStack {
                Text(Riddles.text)
                    .foregroundColor(.black)
                    .font(Font.system(size: 20))
        }
    }
}
@main
struct RiddleWidget: Widget {
    let kind: String = "RiddleWidget"
    var body: some WidgetConfiguration {
        IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
            RiddleWidgetEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
        .supportedFamilies([.systemMedium, .systemLarge])
    }
}
struct RiddleWidget_Previews: PreviewProvider {
    static var previews: some View {
        RiddleWidgetEntryView(entry: SimpleEntry(date: Date(), text: "This Weeks Riddle: ", configuration: ConfigurationIntent()))
            .previewContext(WidgetPreviewContext(family: .systemSmall))
    }
} 

Thanks in advance!

The error is caused by the fact that the Riddles.mediumWidget() and Riddles.largeWidget() methods do not return a SwiftUI View . In SwiftUI, only struct/enum/class types can conform to protocols such as View , which is used to build the UI. To fix this error, you need to modify the Riddles.mediumWidget() and Riddles.largeWidget() methods to return a View instead of setting the Riddles.text property directly.

Here’s an example of how to modify the Riddles.mediumWidget() method to return a Text view:

static func mediumWidget() -> Text {
    Riddles.checkWeekday()
    let userDefaults = UserDefaults(suiteName: "group.SetRiddle")
    if Riddles.isWednesday == true {
        if userDefaults?.value(forKey: "currentRiddle") != nil {
            return Text("\(UserDefaults(suiteName: "group.SetRiddle")?.value(forKey: "currentRiddle") as! String)")
        } else {
            return Text("Please open app to display riddle")
        }
    } else {
        return Text("Please open app to display riddle")
    }
}

You can apply similar changes to the Riddles.largeWidget() method to return a View that can be used to build the UI.

After making these changes, you can then update the RiddleWidgetEntryView to use the returned views from the Riddles.mediumWidget() and Riddles.largeWidget() methods:

struct RiddleWidgetEntryView : View {
    var entry: Provider.Entry
    
    @Environment(\.widgetFamily) var family: WidgetFamily
    
    @ViewBuilder
    var body: some View {
        
        switch family {
        case .systemMedium:
            Riddles.mediumWidget()
                .foregroundColor(.black)
                .font(Font.system(size: 20))
        case .systemLarge:
            Riddles.largeWidget()
                .foregroundColor(.black)
                .font(Font.system(size: 20))
        }
    }
}

With these changes, the error should no longer occur and your code should compile successfully.