storing [Int:String] dictionaries to user.defaults crashes with SIGABRT error

前端 未结 1 1977
春和景丽
春和景丽 2021-01-16 07:47

saving a bunch of data using user.default. everything saves fine except one dictionary [Int:String]. The funny part is this, I can save a dictionary [String:Int] fine, but a

相关标签:
1条回答
  • 2021-01-16 08:22

    The Object that you are trying to save in the UserDefualt must be of type NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. As per apple docs:

    A default object must be a property list—that is, an instance of (or for collections, a combination of instances of) NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData. UserDefault Documentation

    The key of NSDictionary is a string or a type conforming to NSCopying protocol.

    You can try this:

    let yourData = NSKeyedArchiver.archivedDataWithRootObject(yourDictionary)
    let userDefualts = UserDefaults.standard
    userDefualts.set(yourData, forKey: "yourKey")
    
    0 讨论(0)
提交回复
热议问题