尝试Objective-C对象(NSArray、NSDictionary)。
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/UserDefaults/AccessingPreferenceValues/AccessingPreferenceValues.html#//apple_ref/doc/uid/10000059i-CH3-97383
.
可以保存、获取NSData、NSDate、NSString、NSNumber、NSArray、NSDictionary和NSURL类型的对象。
因此,一种可能的解决方案是:
struct Action { var timestamp: TimeInterval; var winner: String }
var actions: [Action] = []
...
actions.map({ ["time": $0.timestamp, "winner": $0.winner ] as NSDictionary })
// You could also convert the actions manually one by one into NSDictionary
// and append them to an [NSDictionary]
UserDefaults.standard.set(actions as NSArray, forKey: "currentMatch")
UserDefaults.standard.synchronize()
Swift数组和字典可以
桥接的
到
NSArray
,
NSDictionary
分别地
您还可以使用Codable,它还可以序列化属性列表。