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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:58:06+00:00 2026-05-26T12:58:06+00:00

I was following the answer for this question ( How To Save Data For

  • 0

I was following the answer for this question (How To Save Data For Bookmarks?) to save a bookmarks into the NSUserDefaults. It is pretty straightforward, and it works properly for the first time (when there’s no entry in the plist). But then it tries to copy the previously saved bookmarks into the new NSMutableArray the app crashes:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '*** -[NSMutableArray initWithCapacity:]: 
method only defined for abstract class.  Define -[__NSArrayM initWithCapacity:]!'

This is my code (a little modified):

-(void) addStationToFavorites{

    // Get the user defaults object
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *userSelectedService = [userDefaults objectForKey:@"idBikeServiceProvider"];

    // Load your bookmarks (editable array)
    NSMutableArray *bookmarks = [[NSMutableArray alloc] initWithCapacity:100];
    NSArray *bookmarksLoaded = [userDefaults arrayForKey:[NSString stringWithFormat: @"favStationForService%@",userSelectedService]];
    if (bookmarksLoaded != nil) {
        [bookmarks initWithArray:bookmarksLoaded];
    } else {
        [bookmarks init];
    }

    // Add a bookmark
    NSMutableDictionary *bookmark = [NSMutableDictionary new];
    [bookmark setValue:pass_stationName forKey:pass_stationId];
    [bookmarks addObject:bookmark];

    // Save your (updated) bookmarks
    [userDefaults setObject:bookmarks forKey:[NSString stringWithFormat: @"favStationForService%@",userSelectedService]];
    [userDefaults synchronize];

    // Memory cleanup
    [bookmarks release];

    //Show feedback
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"Added!"
                                                   delegate:self
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];

}
  • 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-26T12:58:07+00:00Added an answer on May 26, 2026 at 12:58 pm

    This is incorrect:

    // Load your bookmarks (editable array)
    NSMutableArray *bookmarks = [[NSMutableArray alloc]initWithCapacity:100]; // OK
    NSArray *bookmarksLoaded = [userDefaults arrayForKey:[NSString stringWithFormat: @"favStationForService%@",userSelectedService]];
    if (bookmarksLoaded != nil) {
        [bookmarks initWithArray:bookmarksLoaded]; // NOT AGAIN
    } else {
        [bookmarks init]; // NOT AGAIN
    }
    

    Don’t call an init method more than once on an object. I suspect that is the source of your error, as NSArray/NSMutableArray is implemented as a “class cluster” and the exact class that is returned by the initWithCapacity: is not NSMutableArray, but rather some private subclass, which is complaining about being init’d a second time. Here’s a better way to accomplish the same:

    // Load your bookmarks (editable array)
    NSMutableArray *bookmarks;
    NSArray *bookmarksLoaded = [userDefaults arrayForKey:[NSString stringWithFormat: @"favStationForService%@",userSelectedService]];
    if (bookmarksLoaded != nil) {
        bookmarks = [bookmarksLoaded mutableCopy];
    } else {
        bookmarks = [[NSMutableArray alloc] initWithCapacity:100];
    }
    

    The mutableCopy method is very useful when you need a mutable version of an immutable container object. Note that, since “copy” is in the method name, you own the result and must release it later (unless you’re using ARC or Garbage Collection and don’t need to worry about it).

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

Sidebar

Related Questions

I tried following the answer to this question , but could not get xsd.exe
In this answer to this question , Lachlan-Hunt writes the following: With HTML5, you
I'm following along with this useful looking answer to my question . It seems
Note: Question asked to Self Answer ( to save other's time) Cannot install EclipseFP.
I found the following answer to that question: A service opens before you even
I'm trying to answer the following question out of personal interest: What is the
I am using the following code to save additional properties to a model (answer
I still am confused regarding Ajax technology. And I still cannot answer this question
EDIT: ANSWER AT BOTTOM OF THIS QUESTION Okay so I've got some generic EF
I have to answer the following question: Could you think of a scenario in

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.