Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving a trouble dealing with the object returned from NSUserDefaults
    primarykey
    data
    text
    <p>I'm new to ios programming and just started learning this last month.</p> <p>There is one problem that I've been in stuck with for the last two days. I can't figure out how to fix this problem by myself.</p> <p>So, I'd like to ask you a favor to help me solve this problem!</p> <p>This is the error that I'm getting.</p> <pre><code>[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' </code></pre> <p>I learned that I get this error because the object I get from NSUserDefaults is always immutable.</p> <p>So, I mutableCopy the array that I want to add new object to.</p> <pre><code> NSMutableArray *temporaryArray = [users[1][@"users"] mutableCopy]; [temporaryArray addObject:myData]; users[1][@"users"] = temporaryArray; </code></pre> <p>But, on the third line above, of course, it causes the same error.</p> <p>I know the reason why it causes this error but I don't know how to code to solve this problem. I think the solution is something very simple and basic, but I can't figure it out how.</p> <p>I appreciate if anyone helps me to solve this problem!!</p> <p>Thank you very much in advance!</p> <pre><code>- (void)saveData { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSMutableArray *users = [userDefaults objectForKey:@"users"]; NSDictionary *myData = @{@"name":@"test", @"birthdate":@"1981-01-01", @"sex":@"male"}; NSMutableArray *temporaryArray = [users[1][@"users"] mutableCopy]; [temporaryArray addObject:myData]; users[1][@"users"] = temporaryArray; [userDefaults setObject:users forKey:@"users"]; [userDefaults synchronize]; [self.target performSelector:self.action withObject:@"test.png"]; [self dismissViewControllerAnimated:YES completion:nil]; } </code></pre> <p><strong>The structure of "user" Array.</strong></p> <pre><code>( { label = "\U81ea\U5206"; users = ( { birthdate = "2013-10-12"; name = Test; sex = "\U7537\U6027"; } ); }, { label = "Family(\U5bb6\U65cf)"; users = ( ); }, { label = "Friends(\U53cb\U9054)"; users = ( ); } ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload