Mutable Objects inside NSUserDefaults

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

提交回复
热议问题