Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode 保存字典<;字符串,[customStruct]>;设置为用户默认值_Xcode_Swift - Fatal编程技术网

Xcode 保存字典<;字符串,[customStruct]>;设置为用户默认值

Xcode 保存字典<;字符串,[customStruct]>;设置为用户默认值,xcode,swift,Xcode,Swift,如何将字典保存到NSUserDefault?每当我尝试使用时,我都会收到一条错误消息,说明我的字典不符合“AnyObject”协议 NSUserDefaults.standardUserDefaults().setObject([String: [customStruct]](), forKey: "someKey") 从Apple文档中(您应该阅读): NSUserDefaults类为访问常见类型(如浮点、双精度、整数、布尔值和URL)提供了方便的方法。默认对象必须是属性列表,即的实例(或集

如何将字典保存到NSUserDefault?每当我尝试使用时,我都会收到一条错误消息,说明我的字典不符合“AnyObject”协议

NSUserDefaults.standardUserDefaults().setObject([String: [customStruct]](), forKey: "someKey")

从Apple文档中(您应该阅读):

NSUserDefaults类为访问常见类型(如浮点、双精度、整数、布尔值和URL)提供了方便的方法。默认对象必须是属性列表,即的实例(或集合实例的组合):NSData、NSString、NSNumber、NSDate、NSArray或NSDictionary。如果要存储任何其他类型的对象,通常应将其存档以创建NSData的实例。“

因此,如果
customStruct
不是其中之一,则需要将其转换为其中之一,可能是
NSData
NSString