Mutable Objects inside NSUserDefaults

后端 未结 4 1253
说谎
说谎 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:28

    You can directly do by using method insertObject.

    In place of

    [[[arrayTwo objectAtIndex:0] objectForKey:@"itemlar"] addObject:@"hop"];
    

    use this,

    [arrayTwo insertObject:@"hop" atIndex:0];
    

    This will work for as i have tested it's also working finr after that you can make it as immutable object as NSARRAY and save it to NSUSERDEFAULTS.

    0 讨论(0)
  • 2020-12-22 04:29

    NSUserDefaults never returns mutable objects.

    Your code is performing every way of creating a mutable array you can think of (i.e. you're creating a mutable copy of something you just created a mutable copy of), but, you're only dealing with the root container item - not the inner / leaf items. So, when you do objectForKey:@"itemlar"] on your mutable array, you're getting an immutable object back.

    To make it work, you'll need to write your own method that iterates and recurses through the array creating mutable copies at all levels.

    Alternatively, you could look at a 3rd party option like this which digs under the hood of NSUserDefaults to generate mutable containers.

    0 讨论(0)
  • NSUserDefaults is not the right place to store your data. First of all it should only be used for very small amounts of data, such as settings. And secondly it always returns immutable objects, even if you set mutable ones. Making a mutable copy of your first array doesn’t help because only the array will be mutable. Everything that is inside that array isn’t touched by the mutableCopy method and stay immutable.

    You should use the NSPropertyListSerialization class to read and write your data from a file. On reading you can pass options controlling the mutability of the read objects. There you will want to pass NSPropertyListMutableContainers or NSPropertyListMutableContainersAndLeaves.

    With the first all your containers (arrays and dictionaries that is) will be mutable. With the latter also the leaves (that is NSString and NSData objects) will be mutable.

    Depending on how big your data set can get you probably should use a real database or Core Data instead.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题