简体   繁体   中英

How to check if a string in an NSMutableArray isEqualToString X?

Sorry if this is basic, but I can't get my head around this. I have an array and would like to go through every single object in it and see if it isEqualToString:@"something". If tried this, but it will crash:

    for (int i = 0; i < ([myNSMutableArray count]); i++) {

    NSLog(@"i = %i", i);

    if ([[myNSMutableArray objectAtIndex:i] isEqualToString:@"something"]) {
        ...
    } else {
        ...
    }


}

I'll get:

    2011-07-14 13:38:40.983 MNs[21416:207] i = 0
2011-07-14 13:38:40.985 MNs[21416:207] -[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x4c976f0
2011-07-14 13:38:40.987 MNs[21416:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x4c976f0'

Any help would be very much appreciated. Thanks in advance!

EDIT:

Sorry, I forgot. This is how I created the array:

NSMutableArray *myNSMutableArray = [[NSMutableArray alloc] init];

    for (int x = 0; x < 30; x++) {
        [myNSMutableArray addObject:@""];
        [myNSMutableArray addObject:@"something"];
        [myNSMutableArray addObject:@""];
    }

EDIT2:

So sorry, the problem was that I tried to copy a mutable array... so all of your answers are correct, I just need to pick one now, I guess.

Try This,

for (int i = 0; i < ([myNSMutableArray count]); i++) {

    NSLog(@"i = %i", i);

NSString *stringToCheck = (NSString *)[myNSMutableArray objectAtIndex:i];

    if ([stringToCheck isEqualToString:@"something"]) {
        ...
    } else {
        ...
    }


}

From the error message you get, the problem seems lying with how you fill up your myNSMutableArray . In fact,

 [myNSMutableArray objectAtIndex:i]

returns __NSArrayM instead of NSString , hence the error you get.

Could you explain what kind of objects do you add to the NSArray ?

Try

if ([[[myNSMutableArray objectAtIndex:i] stringValue] 
                                         isEqualToString:@"something"])

because elements in array are of ID type. You need to cast to NSString !

And ensure that myNSMutableArray is created correctly and isn't in autorelease!

Alloc + init make array in manual release, instead [NSMutableArray array] is in autorelease.

Hope this helps.

If you have strings there, you should try casting it before trying to compare:

if ([(NSString*)[myNSMutableArray objectAtIndex:i] isEqualToString:@"something"]) {

try like this..

NSMutableArray *myNSMutableArray=[[NSMutableArray alloc]initWithObjects:@"1",@"2",nil];

for (int i = 0; i < ([myNSMutableArray count]); i++) {


    if ([[myNSMutableArray objectAtIndex:i] isEqualToString:@"1"]) 
    {
        NSLog(@"Hello 1");
    } else
    {
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM