iPhone SDK: NSMutableArray unrecognized selector

独自空忆成欢 提交于 2019-12-13 04:41:37

问题


I have an NSMutableArray which gets objects added to it from an NSDictionary. These objects are values of either 1 or 0. I am trying to loop through this NSMutableArray, to check each value. If it is 1, I set add a green tick image to an array, if it is zero, I add a red cross image to an array.

However I am getting the following error on checking the values of the NSMutableArray:

[__NSArrayI isEqualToString:]: unrecognized selector sent to instance

Here is my code:

     facilitiesAvailable = [NSMutableArray new];

     [facilitiesAvailable addObject:[[InfoDictionary valueForKey:@"wc-avail"]copy]];
     [facilitiesAvailable addObject:[[InfoDictionary valueForKey:@"wifi_avail"]copy]];

    tixArray = [NSMutableArray new];

    for(NSString *avail in facilitiesAvailable) {

        if([avail isEqualToString:@"1"]) {

            [tixArray addObject:[UIImage imageNamed:@"greenTick.png"]];
        } else {

            [tixArray addObject:[UIImage imageNamed:@"redCross.png"]];
        }

    }

What am I doing wrong?


回答1:


The message:

 [__NSArrayI isEqualToString:]: unrecognized selector sent to instance

means that avail is an NSArray object and not an NSString as you expected, hence the error. You should check how you populate your InfoDictionary dictionary, since it comes from there.




回答2:


Just out of curiosity, what happens if you replace your code with this:

for(int i = 0; i < [facilitiesAvailable count]; i++) {
    NSString * avail = (NSString *)[facilitiesAvailable objectAtIndex:i];

    if([avail isEqualToString:@"1"]) {
        [tixArray addObject:[UIImage imageNamed:@"greenTick.png"]];
    } else {              
        [tixArray addObject:[UIImage imageNamed:@"redCross.png"]];         
    }
}

This also ASSUMES that the objects being added to facilitiesAvailable are based on the objects in your InfoDictionary truly being NSString objects. This CASTS them to NSString objects, but you will get odd behavior if they arent truly that from the InfoDictionary.



来源:https://stackoverflow.com/questions/11020544/iphone-sdk-nsmutablearray-unrecognized-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!