我正在尝试在具有符合自定义类的成员的对象上使用setValuesForKeys
,这就是为什么我相信我得到以下错误,我怎么能解决这个问题?如果简单,我更喜欢以原生方式执行此操作,或者我可以使用任何库/框架来解决此问题。
此类不是密钥的符号密码值 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()}
]
}
}
}
答案 0 :(得分:1)
KVO很复杂,
我可以清楚地看到错误是&#34; availabilityTimesDict&#34; :强>
您可以使用来自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"