我需要帮助解决错误:
无法使用类型为“(项目:[String],看到:Int)”的参数列表调用类型“QuotesViewController.RandomItems”的初始化程序
这是我的代码:
struct RandomItems: Codable
{
var items : [String]
var seen = 0
init(_ items:[String])
{ self.items = items }
mutating func next() -> String
{
let index = Int(arc4random_uniform(UInt32(items.count - seen)))
let item = items.remove(at:index)
items.append(item)
seen = (seen + 1) % items.count
return item
}
func toPropertyList() -> [String: Any] {
return [
"items": items,
"seen": seen
]
}
}
}
extension QuotesViewController.RandomItems {
init?(propertyList: [String: Any]) {
return nil
}
}
let a = QuotesViewController.RandomItems(items: ["hello"], seen: 2) //<-- Error
let data: Data = try! JSONEncoder().encode(a)
let b = try! JSONDecoder().decode(QuotesViewController.RandomItems.self, from: data)