0

I'm trying to iterate through an NSArray and check if an objectAtIndex is equal to a string.

NSLog(@"%@", myArray) // 3 items. 1 of them is "a"
for (id object in myArray)
{
    NSLog(@"What"); // 3 times
    if ([object isEqual:@"a"]) {
        NSLog(@"Hello"); // Never gets executed
    }
}

The problem is, the NSLog in the if statement never gets executed?

Edit

(
        (
        a
    ),
        (
        01
    ),
        (
        a
    ),
        (
        03
    )
)

When I set it to isEqualToString, I get this error:

2015-03-30 14:42:54.206 MyApp[1575:50954] -[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x7fe721ce3bf0
2015-03-30 14:42:54.215 MyApp[1575:50954] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x7fe721ce3bf0'
7
  • 1
    Show us the full log output. Commented Mar 30, 2015 at 21:47
  • 1
    isEqualToString: is the better choice, but isEqual: should still work. Please post the log so we can see what's actually in myArray. I don't think @"a" is in there. Commented Mar 30, 2015 at 21:50
  • I'll post the output. Commented Mar 30, 2015 at 21:52
  • If both objects are strings, isEqual: and isEqualToString: should both work; the latter is a performance optimization. Commented Mar 30, 2015 at 21:54
  • 1
    And there's your issue. You have an array of arrays of strings. Not an array of strings. Commented Mar 30, 2015 at 21:56

1 Answer 1

1

Your problem is that you have an array that contains three sub-arrays, each of which contain, what is presumably, a single string. You can tell this because of the extra ()'s around the strings in the log output, and by the fact that it's telling you that you tried to send a selector to __NSArrayM.

Here is a quick fix:

NSLog(@"%@", myArray) // 3 items. 1 of them is "a"
for (NSArray *array in myArray)
{
    NSLog(@"What"); // 3 times
    if ([array.firstObject isEqualToString:@"a"])
    {
        NSLog(@"Hello"); // Never gets executed
    }
}

But as others have pointed out you probably want to use isEqualToString: since it will be more performant.

You may also want to reconsider the code that is generating this nested array structure, or the schema that you're using in general, because it seems... unnecessary. Without further information there's not much to be done.

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

2 Comments

Thanks!! This may be out of the scope of the question, but here is the array that I initialized: NArray *myArray = [[NSArray alloc] initWithArray:[self.ingredientsDBManager loadDataFromDB:amountQuery]]; I used appCoda to implement SQLite.
The DB is giving it back to you the way you see it above. You should open a new question if you want help with that; I don't know much about appCoda.

Your Answer

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