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

  • SEARCH
  • Home
  • 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 3454882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:32:45+00:00 2026-05-18T09:32:45+00:00

i have looked all over the place regarding this problem. All i am trying

  • 0

i have looked all over the place regarding this problem. All i am trying to do is, create a nsuser defaults object, then add a mutable array to it and try to modify the nsuserdefaults object.

//Created a sample array
NSMutableArray *xml=[[NSMutableArray alloc] init];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
[xml addObject:@"x"];
//assigned to defaults object which is created previously with a mutableCopy
[defaults  setObject:[xml mutableCopy] forKey:@"userLocationsDetailedXML"]; 
//Tried to modify the defaults object - which at this point should be mutable
[[defaults objectForKey:@"userLocationsDetailedXML" ] replaceObjectAtIndex:1 withObject:@"y"] ;

Adding [defaults synchronize] does’nt help either.

But surprisingly the defaults object is still immutable. It is __NSCFArray, not __NSCFArrayM as expected
Any suggestions?

edit: console output
Class : __NSCFArray => SHould’nt it be _NSCFArrayM since i created a mutable copy?
[
_NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object’

  • 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-18T09:32:46+00:00Added an answer on May 18, 2026 at 9:32 am

    Try casting the array as NSMutableArray, using

    [(NSMutableArray *)[defaults objectForKey:@"userLocationsDetailedXML"] replaceObjectAtIndex:1 withObject:@"y"] ;
    

    that should take care of it.

    EDIT:

    Given that you can’t actually store NSMutableArrays in NSUserDefaults, I recommend that you just go ahead and reassign the entire thing, like so:

    NSMutableArray *xml = [[NSMutableArray alloc] init];
        [xml addObject:@"x"];
        [xml addObject:@"x"];
        [xml addObject:@"x"];
        [xml addObject:@"x"];
        [xml addObject:@"x"];
    
    [defaults  setObject:xml forKey:@"userLocationsDetailedXML"]; 
    [xml release];
    

    Then, for changing the values,

    NSMutableArray *userLoc = [[defaults objectForKey:@"userLocationsDetailedXML"] mutableCopy];
    [userLoc replaceObjectAtIndex:1 withObject:@"y"];
    [defaults  setObject:userLoc forKey:@"userLocationsDetailedXML"]; 
    [userLoc release]; //thanks, dreamlax!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked all over the place but I cant seem to get this
I've looked all over the internet trying to find an answer to my problem,
Ok, I have looked all over and I am having a difficult time trying
I have looked all over and have been at this for about a week
I've looked all over the place, but it seems that examples I have seen
Preface: I have looked all over stackoverflow.com and google for this. I have found
I have looked all over the web and I can not find the information
Every place I have looked all I can find is how to read a
Okay, I have looked hard for this answer, but all the websites, even the
I have a problem over here. You see, I wanted to create my own

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.