Mutable Objects inside NSUserDefaults

后端 未结 4 1270
说谎
说谎 2020-12-22 03:50

I created a simple database and put in NSUserDefaults. My database is NSMutableArray which has dictionaries and arrays inside in it. When I create NSMutableArray from NSUSer

4条回答
  •  春和景丽
    2020-12-22 04:41

    NSUserDefaults, and property lists in general, do not record mutability. When an object is re-created from the file it can be constructed either as a mutable or immutable object (for types which have the option, such as arrays). Unfortunately NSUserDefaults doesn't give you an API call to obtain an immutable object directly.

    Two options you have are (a) create a mutable copy of the object returned by NSUserDefaults or (b) store the object yourself as a property list in a separate file - that way you can read it back as mutable directly.

    For (b) read Apple's docs - it shows how mutability is handled.

提交回复
热议问题