自定义结构类型的变量应该用
@SceneStorage
存储,但它显示编译器错误:
struct MyView {
struct MyData: Identifiable, Codable {
var id: String
init(id: String) {
self.id = id
}
private enum CodingKeys: String, CodingKey {
case id
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
}
}
@SceneStorage var data: [MyData] = [] // Compiler error: "No exact matches in call to initializer"
}
另外符合
RawRepresentable
并不能解决错误:
struct MyView {
...
typealias RawValue = String
init?(rawValue: String) {
guard
let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode(MyData.self, from: data)
else {
return nil
}
self = result
}
public var rawValue: String {
guard
let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return "[]"
}
return result
}
}
为什么这会显示编译器错误?