gpt4 book ai didi

swift - 当为具有符合自定义类的成员的对象设置值时,类不符合键的键值编码

转载 作者:可可西里 更新时间:2023-11-01 02:01:07 25 4
gpt4 key购买 nike

我正在尝试在一个对象上使用 setValuesForKeys,该对象的成员符合自定义类,这就是为什么我相信我会收到以下错误,我该如何解决这个问题?如果简单的话,我更喜欢以 native 方式执行此操作,或者我可以使用任何库/框架来解决此问题。

this class is not key value coding-compliant for the key availabilityTimesDict

func convertJSONDataToSwiftObject(){

let registrationDataDict =
[
"firstName": "Joe Bloggs",
"alternateNames": ["Joe", "Bloggs"],

"availabilityTimesDict" : [
"Friday" : ["times":[["startTime":"9:00 AM","endTime":"1:30 PM"],["startTime":"2:30 PM","endTime":"6:00 PM"]],"weekday":"Friday","available":true],
"Monday" : ["times":[["startTime":"11:00 AM","endTime":"1:30 PM"]], "weekday":"Monday","available":true]]
] as [String : AnyObject]

var profileDetailsObject = ProfileDetails()

profileDetailsObject.setValuesForKeys(registrationDataDict as [String : AnyObject]) // SOURCE OF ERROR

}


class ProfileDetails: NSObject{

var firstName : String!
var alternateNames : [String]!
var availabilityTimesDict : [String : AvailabilityTimes.DailyTimes]!
}

class AvailabilityTimes: NSObject{
struct Times{
var startTime : String?
var endTime : String?

func convertToDictionary() -> Dictionary<String, Any> {
return [
"startTime" : self.startTime as Any,
"endTime" : self.endTime as Any
]
}
}

struct DailyTimes{
let weekday : String
var available : Bool
var times = [Times]()
mutating func update(times: [Times]){
self.times = times
}

func convertToDictionary() -> Dictionary<String, Any> {
return [
"weekday" : self.weekday,
"available" : self.available,
"times" : self.times.map{$0.convertToDictionary()}
]
}

}
}

最佳答案

KVO 很复杂,

我可以清楚地看到错误是 "availabilityTimesDict":

您可以使用来自 kvo dict 的评论 availabilityTimesDict 检查它

如果你使用 initalizer 而不是 KVO 会更好更可读,如果有任何变化,它在未来也会有帮助你可以轻松地做一些修改

这里是示例代码

class ProfileDetails: NSObject{

var firstName : String!
var alternateNames : [String]!
var availabilityTimesDict : [String : AvailabilityTimes.DailyTimes]!

private override init() {
super.init()
}

required init(with FirstName : String,alterName:[String],availabilityTimesDict:[String : AvailabilityTimes.DailyTimes]) {
super.init()
self.firstName = FirstName;
self.alternateNames = alterName;
self.availabilityTimesDict = availabilityTimesDict;

}
}

class AvailabilityTimes: NSObject{
struct Times{
var startTime : String?
var endTime : String?

func convertToDictionary() -> Dictionary<String, Any> {
return [
"startTime" : self.startTime as Any,
"endTime" : self.endTime as Any
]
}
}

struct DailyTimes{
let weekday : String
var available : Bool
var times = [Times]()
mutating func update(times: [Times]){
self.times = times
}

func convertToDictionary() -> Dictionary<String, Any> {
return [
"weekday" : self.weekday,
"available" : self.available,
"times" : self.times.map{$0.convertToDictionary()}
]
}

}
}

var availTimes = AvailabilityTimes.Times.init(startTime: "9:00 AM", endTime: "1:30 PM")
var avaiblityTimes = AvailabilityTimes.DailyTimes.init(weekday: "Friday", available: true, times:[availTimes] )

var times = ["Firday":avaiblityTimes]

var profileDetailsObject = ProfileDetails.init(with: "Joe Bloggs", alterName: ["Joe", "Bloggs"], availabilityTimesDict: times)
profileDetailsObject.firstName = "test"

关于swift - 当为具有符合自定义类的成员的对象设置值时,类不符合键的键值编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46269503/

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