I have a mutable array (self.arr1) that allows users to add objects to it. In this example, the self.arr1 is saved to NSUserDefaults, and looks like this:
(
(
(
"Park"
),
Corner Store
),
"Cafe"
),
"Brewery"
)
I'm using the below code to add objects to self.arr1 (ie. when button is tapped, add objects to self.arr1), and then add self.arr1 to NSUserDefaults. I then want to check if "Park" is present in NSUserDefaults the next time the user opens the app. Even though it is present, the code is executing as if it's not there. It's almost as if because I'm initializing a new array everytime the button is tapped, it doesnt see that Park is indeed present in self.arr1. How can I have my code check all values inside self.arr1?
If I don't initialize the array when the button is tapped, it doesnt allow me to add objects at all, and the array returns null.
ViewController.m
-(void)viewDidLoad {
if ([self.placeDefaults containsObject:self.locationName.text] {
// DO SOEMTHING
}
}
- (IBAction)collectPoints:(id)sender {
self.arr1 = [[NSMutableArray alloc] init];
[self.arr1 addObject:arrayOfPlaces];
self.placeDefaults = [NSUserDefaults standardUserDefaults];
[self.arr1 addObject:self.savedTitle];
[self.placeDefaults setObject:self.arr1 forKey:@"visitedPlaces"];
}
self.arr1 = [arrayOfPlaces mutableCopy];and then add the new string, but 1). Storing arrays inNSUserDefaultsisn't a great data persistence strategy and 2) If you are just getting started, start with Swift rather than Objective C.