How do I check if an object exists at a certain index of an NSMutableArray?

混江龙づ霸主 提交于 2019-12-10 12:55:08

问题


For instance, how could I verify if there is an item at the index 3?

The objects in the NSArray are instantiated from the class "Animal."


回答1:


Well, since NSMutableArray has to hold non-nil objects, as long as the array is big enough, you know there's something at index i:

if ([myArray count] > 3) {
    id myObj = [myArray objectAtIndex:3];
    ...
}

If you needed to check something elsek, like say make sure it didn't have a reference to the NSNull singleton, you could then check

if (myObj != [NSNull null]) ...



回答2:


Since there can be no 'gaps' in a NSMutableArray's storage, if your index is less than [array count], you can be certain an object is present at that index.




回答3:


try this code

for(int j = 0; j < [yourArray count]; j++)
{
    if(obj isKindOfClass:[Animal class]]) {
        return
    }
}


来源:https://stackoverflow.com/questions/10117543/how-do-i-check-if-an-object-exists-at-a-certain-index-of-an-nsmutablearray

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