Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.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
Objective c NSMutableArray包含对象问题_Objective C_Cocoa_Nsmutablearray - Fatal编程技术网

Objective c NSMutableArray包含对象问题

Objective c NSMutableArray包含对象问题,objective-c,cocoa,nsmutablearray,Objective C,Cocoa,Nsmutablearray,我尝试比较两个数组,假设数组a是一系列数组,如下所示: ((1,1), (1,2), (1,3), (1,4), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3)) 数组b是一系列数组,如下所示: ((1,1), (1,2), (1,3), (1,4), (2,1), (2,2), (2,3)) 如何检查数组a中的项目是否不在数组b中 for(NSString* itemFromA in a) { if ([b containsObject: itemFr

我尝试比较两个数组,假设数组a是一系列数组,如下所示:

((1,1), (1,2), (1,3), (1,4), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3)) 
数组b是一系列数组,如下所示:

((1,1), (1,2), (1,3), (1,4), (2,1), (2,2), (2,3))
如何检查数组a中的项目是否不在数组b中

for(NSString* itemFromA in a) {
 if ([b containsObject: itemFromA] == FALSE) { 
   NSLog(@"ok");
   break;
 }
}

这似乎对我不起作用

我不确定迭代器类型是否正确;听起来b和a是数组的数组,但您使用NSString*作为迭代器类型。我不是Objective-C内部的高手,但我相信这会导致检查失败,因为itemFromA的类型为NSString*,并且您正在迭代NSArray*的集合


此外,containsObject:可能没有执行您想要的操作,即它使用isEqual来执行比较。文档中提到isEqual专门用于NSArray,但我找不到有关该专门化的详细信息。我不知道ContainesObject:在这种深度比较操作中会有怎样的表现。

我解决了这个问题,不过请随意清理一下

@应用控制器的实现 -IBActionGetArrayData:idsender{

int a=10;
int b=10;
int c=20;
int d=20;
int row=0;
int col=0;

NSMutableArray* e = [[NSMutableArray alloc] init];
NSMutableArray* f = [[NSMutableArray alloc] init];

for(row=0; row<a; row++) {
    for(col=0; col<b; col++) {
        NSNumber* thisrow = [NSNumber numberWithInt:row];
        NSNumber* thiscol = [NSNumber numberWithInt:col];
        [e addObject:[NSArray arrayWithObjects:thisrow, thiscol, nil]];
    }
}

for(row=0; row<c; row++) {
    for(col=0; col<d; col++) {
        NSNumber* thisrow = [NSNumber numberWithInt:row];
        NSNumber* thiscol = [NSNumber numberWithInt:col];
        [f addObject:[NSArray arrayWithObjects:thisrow, thiscol, nil]];

    }
}

//check to see if f contains items from e
for(NSString* thisset in f) {
    if ([e containsObject: thisset]) {
        NSLog(@"This set (%@) is already being used.", thisset);
    } else {
        NSLog(@"We can start the ad at these coordinates: %@", thisset);
        break;
    }
}
[e释放]; [f释放]; }
@结束

请注意正确设置问题的格式。请将代码的格式设置为可以阅读的格式。另外,由于您的描述与您的代码不太匹配,所以不清楚您在问什么。for循环使它看起来像数组A和数组B包含NSString实例,但您的描述表明每个数组都是由未识别的字符串组成的数组?一个号码?包含NSRange结构的NSValue?。请澄清确切的结构。你不应该将其与false进行比较。永远使用![b containsObject:itemFromA]。在与false进行比较的情况下,这没有什么区别。你不被允许做的,是比较真实的。顺便说一句:在Objective-C中,我们使用是和否,而不是真和假。@Georg:虽然我同意你对BOOL的是/否,但如果[b containsObject:itemFromA]==否是完全有效的-containsObject:返回BOOL,这就像说BOOL isThere=YES;如果isThere==否。。。这就好像!数组中有什么类型的对象?自定义对象?简单地说,在Pruittgoe澄清确切的数据结构之前,这个问题无法得到任何肯定的回答。由于Objective-C的动态特性,即使在指定错误的数据类型时,代码也会调用正确的方法。我认为在运行时这并不重要,因为NSArray和NSString都是NSObject的子类,它包含相等和散列方法。由于数组中的任何内容都将使用它们自己的实现来回答这些问题,因此无论您指定哪个类来保存指针,它们的行为都是相同的。问题是我们根本不知道问题/代码的确切结构是什么。这很重要,因为,例如,NSArray的-isEqual:依赖于不同于NSString的标准。实际上,指针的静态类型并不决定将使用什么实际方法实现,这将由实例的动态类型在运行时确定。