0

in my previous question i was given some code in order to make up this statment.

favoriteArray is an Mutable Array...

if (![self.favoritesArray containsObject:@"added"])
    {
        [self.favoritesArray addObject:@"added"];
    }

else
{
    [self.favoritesArray removeObject:@"added"];
}
//NSUInteger newRow = [self.favoritesArray count];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self.favoritesArray forKey:@"MyFavorites"]; 

however i can't get @"added" to be removed... when the app loads it automatically loads added in the favorites the added text even after cleaning the code. when i press the button it doesnt remove it. so im guessing there is something wrong

and here is where i load it...

   NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   NSMutableArray *didContain = [[NSMutableArray alloc] init];
   didContain = [[userDefaults objectForKey:@"MyFavorites"] mutableCopy];

   if ([didContain count] == 0) {
       NSLog (@"zero");

            //
            // no favorites have ever been saved
            //


    } else {

        // load the favorites into some array you synthesized just like before
        self.tableFavoritesData = [[NSMutableArray alloc] init];
        self.tableFavoritesData = [[userDefaults objectForKey:@"MyFavorites"] mutableCopy];
    }
4
  • Can you explain better what's wrong with your code? Commented Aug 21, 2010 at 11:20
  • You store/load the array in/from the user defaults? If so, how do you load it? And when & how are you checking wether @"added" was removed. Commented Aug 21, 2010 at 11:28
  • Your code leaks a NSMutableArray. Commented Aug 21, 2010 at 11:37
  • You alloc/init an empty NSMutableArray, and never release it but just replace it by the next one. You do this twice... Commented Aug 21, 2010 at 13:03

2 Answers 2

3

Your code doesn't make much sense.

To load the array from the defaults:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
self.tableFavoritesData = [[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];

Is all there is to do. If you don't have an empty array set up as a default value, you might check for nil:

if (!self.tableFavouritesData)
    self.tableFavoritesData = [[NSMutableArray alloc] init];

Or you won't be able to put anything in it as it will still be nil. (Alternatively use registerDefaults: to register a default value. This is the preferred way and you must do that before actually reading from the defaults, i.e. directly at startup).

Sign up to request clarification or add additional context in comments.

1 Comment

Assuming that self.tableFavoritesData is a retaining property, you probably want to use self.tableFavoritesData = [NSMutableArray array]; - otherwise you will leak memory.
0

Just try to add @"added" after the array creation and look if it gets removed printing the array out. Maybe you are not serializing it correctly or adding the value in the right way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.