1

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"];
    
}
5
  • How do you read an array with 3 open brackets and 4 close brackets? Commented Dec 4, 2020 at 3:14
  • @ElTomato I literally just copied and pasted what the console spit out when NSUserDefaults was logged. Must've missed one. Commented Dec 4, 2020 at 3:19
  • 1
    Yes, your data structure isn't clear; From your code it seems you keep adding the existing array as a single value (so a nested array) to the new array followed by the new string as a single value. You probably want self.arr1 = [arrayOfPlaces mutableCopy]; and then add the new string, but 1). Storing arrays in NSUserDefaults isn't a great data persistence strategy and 2) If you are just getting started, start with Swift rather than Objective C. Commented Dec 4, 2020 at 3:20
  • Ahh self.arr1 = [arrayOfPlaces mutableCopy]; is exactly what I was looking for - THANK YOU! I'm not just starting (aeons deep into an older Obj-C project). That said, I know plenty of devs who still prefer Obj-C to Swift; changing strongly advised? @Paulw11 Commented Dec 4, 2020 at 3:26
  • 1
    Apple is pretty clear that Swift is the way forward. Objective C isn't going away but it doesn't get access to new technologies like SwiftUI Commented Dec 4, 2020 at 3:31

1 Answer 1

1

Your code is adding the existing array as a nested array and then adding the new single string to the end.

All you need to do is make a mutable copy of the existing array and then add the new value. Also there is no need to use properties when local variables will do.

- (IBAction)collectPoints:(id)sender {
    
     NSMutableArray newArray = [[arrayOfPlaces mutableCopy];
                                                                            
     [newArray addObject:self.savedTitle];

     NSUserDefaults *placeDefaults = [NSUserDefaults standardUserDefaults];
                                                                            
     [placeDefaults setObject:newArray forKey:@"visitedPlaces"];
    
}
Sign up to request clarification or add additional context in comments.

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.