gpt4 book ai didi

Swift:无法将类型 '[String : DayData]' 的值转换为预期的参数类型 'AnyObject?'

转载 作者:行者123 更新时间:2023-11-28 16:16:18 25 4
gpt4 key购买 nike

我在尝试保存我创建的新结构时无法保存到 CoreData。

var allInformationByDate = [
"2016-08-13": DayData(sales: 0, doorsKnocked: 0, milesWalked: 0.00, hoursWorked: 0.00)

]

struct DayData {
let sales: Int
let doorsKnocked: Int
let milesWalked: Double
let hoursWorked: Double
}

我收到错误:

Cannot convert value of type '[String : DayData]' to expected argument type 'AnyObject?'

关于这段代码,特别是关于“allInformationByDate”...

var allInfoByDateDefault = NSUserDefaults.standardUserDefaults()
allInfoByDateDefault.setValue(allInformationByDate, forKey:"allInfoByDateRecord")
allInfoByDateDefault.synchronize()

有人知道如何修正这个语法吗?我试过改成

allInformationByDate as! AnyObject

但这只会让应用程序崩溃。

最佳答案

问题是您无法在 NSUserDefaults 中保存自定义对象

但由于 DayData 的所有属性都符合属性列表,您可以编写 NSUserDefaults 的扩展以将 DayData 转换为字典,反之亦然.

此扩展可以编写单个 DayData 对象以及字典 [String:DayData]

extension NSUserDefaults {

// read a write a single 'DayData' object

func dayDataForKey(key: String) -> DayData? {
guard let data = self.objectForKey(key) as? [String:AnyObject] else { return nil }
return DayData(sales: data["sales"] as! Int, doorsKnocked: data["doorsKnocked"] as! Int, milesWalked: data["milesWalked"] as! Double, hoursWorked: data["hoursWorked"] as! Double)
}

func setDayData(dayData : DayData, forKey key: String) {
let propertyListRepresentation = ["sales": dayData.sales, "doorsKnocked" : dayData.doorsKnocked, "milesWalked": dayData.milesWalked, "hoursWorked": dayData.hoursWorked]
self.setObject(propertyListRepresentation, forKey: key)
}

// read a write a dictionary ('[String:DayData]') object

func dayDataDictionaryForKey(key: String) -> [String: DayData]? {
guard let dayData = self.objectForKey(key) as? [String : [String: AnyObject]] else { return nil }
var result = [String: DayData]()
for (key, value) in dayData {
result[key] = DayData(sales: value["sales"] as! Int, doorsKnocked: value["doorsKnocked"] as! Int, milesWalked: value["milesWalked"] as! Double, hoursWorked: value["hoursWorked"] as! Double)
}
return result
}

func setDayDataDictionary(dayData : [String: DayData], forKey key: String) {
var result = [String : [String: AnyObject]]()
for (key, value) in dayData {
result[key] = ["sales": value.sales, "doorsKnocked" : value.doorsKnocked, "milesWalked": value.milesWalked, "hoursWorked": value.hoursWorked]
}
self.setObject(result, forKey: key)
}
}

现在您可以轻松地为用户默认值编写字典:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setDayDataDictionary(allInformationByDate, forKey: "allInfoByDateRecord")

或阅读它

let defaults = NSUserDefaults.standardUserDefaults()
if let infoByDateDefault = defaults.dayDataDictionaryForKey("allInfoByDateRecord") {
allInformationByDate = infoByDateDefault
}

关于Swift:无法将类型 '[String : DayData]' 的值转换为预期的参数类型 'AnyObject?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39032242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com