ios - Casting struct to NSUserDefaults in Swift? -
is there way can cast swift data set in someway form acceptable nsuserdefauts
? i.e. nsobject
nsset
? (p.s. realize nsuserdefaults
isn't type of data, i'm testing)
struct users { var name: string = "" var stores: [store] } struct store { var name: string = "" var clothingsizes = [string : string]() } init() { let userdefaults = nsuserdefaults.standarduserdefaults() if let userspeople = userdefaults.valueforkey("users") as?
i think can use dictionary. you'll need make method wrap data struct , vice versa.
for example:
var users : [string: anyobject]() users["name"] = "somename" users["stores"] = yourstorearray nsuserdefaults.standarduserdefaults().setobject(users, forkey: "users")
something that.
and when need struct
if let mydictionaryfromud = userdefaults.objectforkey("users") as? [string:anyobject]{ self.users = users(mydictionaryfromud["name"], stores: mydictionaryfromud["stores"] as! [store]) }
i aslo assume save userdefaults array of users. in case, save [[string: anyobject]]
mechanics same.
Comments
Post a Comment