In the code below, I am checking to see if queso is in the grocery list. At first it isn’t, but then I add it with the addObject method.
The problem is that either way before I added it to the array and after I add it, I still get the same answer of NO (‘0’) returned. Here’s my code. Can anyone tell me what I am doing wrong in my code. It is like the second time that I call it, it is being skipped.
// Create some grocries
NSString *salsa = [NSString stringWithString:@"Texas Texas Salsa"];
NSString *queso = [NSString stringWithString:@"The Best Queso"];
NSString *chips = [NSString stringWithString:@"Our Chips"];
// Create the mutable array
NSMutableArray *groceryList = [NSMutableArray array];
// Add groceries to the list
[groceryList addObject:chips];
[groceryList addObject:salsa];
//Try out the containsObject: method for my array
BOOL quesoIsOnTheList = [groceryList containsObject:queso];
NSLog(@"Is queso on the list? %i", quesoIsOnTheList);
// Forgot to put queso in the query, add it to the top of the list
[groceryList insertObject:queso atIndex:0];
// Now check again after queso has been added
NSLog(@"Now is queso on the list? %i", quesoIsOnTheList);
You’re defining quesoIsOnTheList before you insert queso in the array, and after you add queso, you’re logging that same variable, so of course it’s still going to be NO. Replace that second log with:
NSLog(@"Now is queso on the list? %i",[groceryList containsObject:queso]);It should now return
YES.