Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4064436
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:51:19+00:00 2026-05-20T15:51:19+00:00

I have an NSMutableArray saved with NSUserDefaults. This array is my favourite items that

  • 0

I have an NSMutableArray saved with NSUserDefaults.
This array is my “favourite” items that the user can saves, so when i want to add one item, i need to read the array (from NSuserDefault) and save in the first free position.

I’m using this method to add a value in the NSMutableArray

-(IBAction)save{
   NSMutableArray *abc = [[NSUserDefaults standardUserDefaults] objectForKey:@"12345"];
   int n = [abc count];
   [abc insertObject:@"aaa" atIndex:n];
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   [[NSUserDefaults standardUserDefaults] setObject:abc forKey:@"12345"];
   [defaults synchronize];
   [abc release];
}

what’s the deal?
That if the user call this method two times, the second time the app crashes with this log:

* Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object’

why?
and why just the second time?
The first time works fine!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T15:51:20+00:00Added an answer on May 20, 2026 at 3:51 pm

    NSUserDefaults always returns immutable objects, even if the original object was mutable. It’s in the documentation for objectForKey:

    The returned object is immutable, even if the value you originally set was mutable.

    You will need to create a copy of the returned object before you modify it, using [NSMutableArray arrayWithArray:]

    Probably also best to use the arrayForKey method of NSUserDefaults if you’re retrieving an array. Docs here: https://developer.apple.com/documentation/foundation/userdefaults

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
I have a class that subclasses NSMutableArray. I init it using: MyClass class =
I have a plist (array of dictionaries) with 20 items, each of the 20
Have you managed to get Aptana Studio debugging to work? I tried following this,
I have a TabBar, NavBar, SearchBar with ScopeBar on my screen. I can search
I have a JSON return being formatted and saved into a plist, it looks
I´m new in iPhone development and I have this memory leak. I´m using an
I'm trying to save the kABFirstNamePropert, kABLastNameProperty and kABAddressProperty all saved into an array
I have an NSMutableArray which has about 18 objects. They are in a specific
I have a UITabBarController. One of the tab shows application bookmarks, these bookmarks are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.