I’m making this app with a tableview and stuff. I want to make a “Favorites”-tab, and in the detail-view I want to make a star-like button. I’m done with the “add to favorites” stuff and want to make it impossible to add a string to my favoriteViewController more than once.
Basically, I want to check if my NSUserDefaults contains a certain string in its NSMutableArray that matches the indexPath.row in the table view. It’s really hard to explain…
Here’s a snippet out of the code. I want to check if the “indexPathRowString” already exists in favoriteKey, before adding it again.
-(IBAction)addToFavorite:(id)sender {
NSMutableArray* alreadyFavourites = [[[NSUserDefaults standardUserDefaults] objectForKey:@"favoriteKey"] mutableCopy];
[favoritedAlready addObject:indexPathRowString];
[[NSUserDefaults standardUserDefaults] setObject:alreadyFavourites forKey:@"favoriteKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I’m very thankful for answers, I really need to fix this! 🙂
You want NSArray’s
containsObject:.Because NSString implements the NSObject Protocol method
isEqual:to returnYESfor two strings of the same content, this method will work correctly for you with two different string objects of the same value.