简体   繁体   English

类型 'Favorites.Type' 不能符合 'Encodable'; 只有结构/枚举/类类型可以符合协议

[英]Type 'Favorites.Type' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols

Please tell me what could be the problem with this error and how to fix it?请告诉我这个错误可能是什么问题以及如何解决它?

I'm use SwiftUI 2.0我正在使用 SwiftUI 2.0

"Type 'Favorites.Type' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols" “类型 'Favorites.Type' 不能符合 'Encodable';只有 struct/enum/class 类型可以符合协议”

Code:代码:

class Favorites: ObservableObject {

private var tasks: Set<String>
let defaults = UserDefaults.standard

init() {
    let decoder = JSONDecoder()
    if let data = defaults.value(forKey: "Favorites") as? Data {
        let taskData = try? decoder.decode(Set<String>.self, from: data)
        self.tasks = taskData ?? []
    } else {
        self.tasks = []
    }
}

func getTaskIds() -> Set<String> {
    return self.tasks
}

func isEmpty() -> Bool {
    tasks.count < 1
}

func contains(_ task: dataTypeFont) -> Bool {
    tasks.contains(task.id)
}

func add(_ task: dataTypeFont) {
    objectWillChange.send()
    tasks.insert(task.id)
    save()
}

func remove(_ task: dataTypeFont) {
    objectWillChange.send()
    tasks.remove(task.id)
    save()
}

func save() {
    let encoder = JSONEncoder()
    if let encoded = try? encoder.encode(Favorites)  {
        defaults.set(encoded, forKey: "Favorites")
    }
}

} }

Screenshot Error: Error截图错误:错误

Typo.错字。

According to the load method you have to encode tasks not the class type根据load方法,您必须编码tasks而不是类类型

func save() {
    let encoder = JSONEncoder()
    if let encoded = try? encoder.encode(tasks)  {
        defaults.set(encoded, forKey: "Favorites")
    }
}

And don't use value(forKey: with UserDefaults , there is a dedicated method并且不要使用value(forKey: with UserDefaults ,有专门的方法

if let data = defaults.data(forKey: "Favorites") {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“MovieSearchContainer.Type”不能符合“Decodable”; 只有结构/枚举/类类型可以符合协议 - Type 'MovieSearchContainer.Type' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols 类型 &#39;()&#39; 不能符合 &#39;View&#39;; 只有 struct/enum/class 类型才能符合协议; 使用 SwiftUI 调用函数 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols; calling functions with SwiftUI 类型 &#39;() -&gt; ()&#39; 不能符合 &#39;View&#39;; 只有结构/枚举/类类型可以符合协议 - Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 协议类型'*'的值不能符合'*'; 只有结构/枚举/类类型可以符合协议 - Value of protocol type '*' cannot conform to '*'; only struct/enum/class types can conform to protocols 使用 if 语句时:类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - While using an if statement: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 如何修复类型“()”不能符合“视图”; 只有结构/枚举/类类型可以符合协议 - how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols “类型‘()’不能符合‘视图’; 只有结构/枚举/类类型可以符合协议” - “Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols” type() 不能符合 View; 只有结构/枚举/类类型可以符合协议 - Type () cannot conform to View; only struct/enum/class types can conform to protocols
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM