Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSArray中索引处的对象_Iphone_Ios_Arrays - Fatal编程技术网

Iphone NSArray中索引处的对象

Iphone NSArray中索引处的对象,iphone,ios,arrays,Iphone,Ios,Arrays,我有一个数组。我想检查特定索引中是否存在对象。如何做到这一点?如果您只想检查是否有对象,请提供帮助。 if (myIndex < [array count]) 如果您想知道某个索引处的对象是否属于某个类 [[array objectAtIndex:myIndex] isKindOfClass:[TheClassToCompareTo class]]; 像这样检查 if([array objectAtIndex:i]!= nil) { NSLog("Object present"); }

我有一个数组。我想检查特定索引中是否存在对象。如何做到这一点?如果您只想检查是否有对象,请提供帮助。

if (myIndex < [array count])
如果您想知道某个索引处的对象是否属于某个类

[[array objectAtIndex:myIndex] isKindOfClass:[TheClassToCompareTo class]];
像这样检查

if([array objectAtIndex:i]!= nil)
{
NSLog("Object present");
}
else{
NSLog("Object Not Present")
}
if(i<=[array count]){
    if([array objectAtIndex:i]!= nil)
    {
    NSLog("Object present");
    }
    else{
    NSLog("Object Not Present")
    }
}
@implementation NSArray (Safe)

- (id)safeObjectAtIndex:(NSUInteger)index {
    if (index >= [self count]) return nil;
    return [self objectAtIndex:index];
}

@end
修改: 你应该这样做

if([array objectAtIndex:i]!= nil)
{
NSLog("Object present");
}
else{
NSLog("Object Not Present")
}
if(i<=[array count]){
    if([array objectAtIndex:i]!= nil)
    {
    NSLog("Object present");
    }
    else{
    NSLog("Object Not Present")
    }
}
@implementation NSArray (Safe)

- (id)safeObjectAtIndex:(NSUInteger)index {
    if (index >= [self count]) return nil;
    return [self objectAtIndex:index];
}

@end

如果(i
BOOL exists=index<[array count]?是:否;
您应该检查数组的长度(使用
count
方法),并且给定
NSArray
不能包含
nil
因此它必须包含以下内容:


首先必须检查该对象的索引是否小于数组的大小,然后在该索引处查询数组

if (index < [array count] && [array objetAtIndex:index]){
/* Your code*/
}
if(索引<[array count]&&&[array objetAtIndex:index]){
/*你的代码*/
}

您可以使用
containsObject
方法检查数组是否包含特定对象。如果包含,则通过
indexOfObject
方法获取其索引

if ([yourArrayArray containsObject:yourObject]) 
{
    NSLog(@"Found");
    int index = [yourArray indexOfObject:yourObject];     
}
使用方法

if ([Array indexOfObject:object]==index) {
        //code
    }

我知道这是一条老线索,但我只是想帮忙

您可以将一个类别添加到
NSArray
中,如下所示

if([array objectAtIndex:i]!= nil)
{
NSLog("Object present");
}
else{
NSLog("Object Not Present")
}
if(i<=[array count]){
    if([array objectAtIndex:i]!= nil)
    {
    NSLog("Object present");
    }
    else{
    NSLog("Object Not Present")
    }
}
@implementation NSArray (Safe)

- (id)safeObjectAtIndex:(NSUInteger)index {
    if (index >= [self count]) return nil;
    return [self objectAtIndex:index];
}

@end

如果在引用中检查对象是否为nil而不是null:如果索引超出数组末尾(即,如果索引大于或等于count返回的值),则会引发NSRangeException。您可以使用
nil
。因此可以使用[array count]用于计算数组元素并使用if conditionNope;首先,
objectAtIndex
将引发一个异常,如我的第一条评论中所述,
NSArray
不能包含
nil
,因此这也不起作用。同样,
NSArray
也不能包含
nil
参考:如果索引超出了e数组的nd(即,如果索引大于或等于count返回的值),引发了NSRangeException。
NSArray
不能包含
nil
。您的意思是要检查特定索引中是否存在特定对象,还是要检查特定索引中是否存在任何对象?@gtm我要检查特定索引中的任何对象。谢谢您的意思是
如果([Array indexOfObject:obj]==index)
?您还可以删除
-[NSArray containsObject:][/code>行,并检查
-[NSArray indexOfObject:][/code>是否返回
NSNotFound