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 8103465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:33:21+00:00 2026-06-05T23:33:21+00:00

i`m trying to make an NSMutableArray From NSUserDefaults so i can add/delete and edit

  • 0

i`m trying to make an NSMutableArray From NSUserDefaults so i can add/delete and edit it later

my code is

- (void)viewDidLoad {
[super viewDidLoad];

NSUserDefaults *ArrayTable = [NSUserDefaults standardUserDefaults];
[ArrayTable setObject:@"One" forKey:@"myArray"];
[ArrayTable setObject:@"Two" forKey:@"myArray"];
[ArrayTable setObject:@"Three" forKey:@"myArray"];
[ArrayTable setObject:@"Four" forKey:@"myArray"];
[ArrayTable setObject:@"Five" forKey:@"myArray"];
[ArrayTable synchronize];

NSMutableArray *array = [[NSMutableArray alloc] init];
array = [ArrayTable objectForKey:@"myArray"];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
}

when i build and run nothing shows up

i googled it but with no help, i`m sure i didn’t understand how to do it

what i need is to build an app that contain a tableview with empty data, the use will fill in the data Add/Delete/Edit

can someone please explain it for me

thank you in advance

  • 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-06-05T23:33:23+00:00Added an answer on June 5, 2026 at 11:33 pm

    First, you assigned your array from the user defaults to a local variable named array. Assuming you have a property for this class also named array, this local assignment masks that. If it had not, you would have crashed when you tried to call -count on a string.

    The NSUserDefaults object is a dictionary. Each time you call -setObject:forKey: on it, you are actually replacing the object previously set for that key. So at the end of your series of calls to -setObject:forKey:, the resulting value is the NSString Five.

    You can’t really store a mutable object in the NSUserDefaults, instead you would take a mutable copy of the object when you assign it to your local variable or ivar. To get the behavior you are probably expecting, you should do something like the following:

    - (void)viewDidLoad
    {
      // Create the array template and store it in NSUserDefaults
      NSArray* arrayTemplate = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", nil];
      NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
      [defaults setObject:arrayTemplate forKey:@"myArray"];
      [defaults synchronize];
    
      // Retrieve a mutable copy of the array from user defaults and assign it to the
      // the property 'array' <- note, this should be an NSMutableArray
      self.array = [[defaults objectForKey:@"myArray"] mutableCopy]; // if not using arc, autorelease this here
    }
    

    With that example code, it should behave the way you expected it to behave, and you can continue on. Obviously it makes no sense to set the array in -viewDidLoad and then immediately read a mutable copy. The key thing to keep in mind is that -setObject:forKey: will always replace any object already set for that key. It doesn’t add elements or anything like that.

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

Sidebar

Related Questions

I'm trying make this code which is from a Dictionary of Arrays (CODE) from
I am trying to add images that have been stored to a NSMutableArray. As
I'm trying to make an array of objects (animalImages) from a plist file which
I'm trying to make an NSMutableArray usable in multiple classes. I'm having an issue
I am trying to make a tableView that will allow me to delete and
am trying to add an array i get from NSURL into an array using
I am trying make long screen to vertical direction. So, I need a screen
Trying to make a custom :confirm message for a rails form that returns data
trying to make a page which will recursively call a function until a limit
Trying to make a floating css menu. How do I get the floating div

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.