Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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 从另一个数组中减去一个NSArray中的对象_Objective C_Cocoa_Cocoa Touch_Nsarray_Subtraction - Fatal编程技术网

Objective c 从另一个数组中减去一个NSArray中的对象

Objective c 从另一个数组中减去一个NSArray中的对象,objective-c,cocoa,cocoa-touch,nsarray,subtraction,Objective C,Cocoa,Cocoa Touch,Nsarray,Subtraction,我有两个理由: NSArray *wants = [NSArray arrayWithObjects: @"apples", @"oranges", @"pineapple", @"mango", @"strawberries", nil]; NSArray *needs

我有两个理由:

NSArray *wants = [NSArray arrayWithObjects:
                  @"apples", 
                  @"oranges", 
                  @"pineapple", 
                  @"mango", 
                  @"strawberries", 
                  nil];
NSArray *needs = [NSArray arrayWithObjects:
                  @"apples", 
                  @"pineapple", 
                  @"strawberries", 
                  nil];
我想要
XOR
它们。像
想要-需要
这样的东西,我剩下的就是

[NSArray arrayWithObjects:
@"oranges", 
@"mango", 
nil];
我通常会经历一些沉重的循环,但我相信有一个更实际的方法。我应该怎么做呢?

像这样的事情

NSMutableArray *array = [NSMutableArray arrayWithArray:wants];
[array removeObjectsInArray:needs];

Kirby的答案很好,但是:如果您不关心数组中元素的顺序,那么应该使用集合。如果顺序很重要,你可以考虑<代码> nSoRoDeDeSE> <代码>。您可以使用
-minusSet:
或后者的
-minusOrderedSet:
方法。

尝试以下方法:

NSArray *NSArray_XOR(NSArray *arr1, NSArray *arr2)
{
    NSMutableArray *results = [NSMutableArray array];

    for (int i = 0; i < arr1.count; i++) {
        id obj = [arr1 objectAtIndex:i];

        if (![arr2 containsObject:obj])
            [results addObject:obj];
    }

    for (int i = 0; i < arr2.count; i++) {
        id obj = [arr2 objectAtIndex:i];

        if (![arr1 containsObject:obj])
            [results addObject:obj];
    }

    // make a unmutable copy of the array.
    return [NSArray arrayWithArray:results];
}
NSArray*NSArray\u XOR(NSArray*arr1,NSArray*arr2)
{
NSMutableArray*结果=[NSMutableArray];
for(int i=0;i
如果有两个假设:顺序不重要(或者可以恢复,例如,如果现在对数组进行排序)*并且没有项目在任一数组中出现一次以上(尽管可以使用计数集),则集合可能是一个不错的选择

两个集合的XOR(严格地说,对称差)是并集减去交集:

NSMutableSet * unioned = [NSMutableSet setWithArray:wants];
[unioned unionSet:[NSSet setWithArray:needs]];
NSMutableSet * intersection = [NSMutableSet setWithArray:needs];
[intersection intersectSet:[NSSet setWithArray:wants]];

[unioned minusSet:intersection];


*如果顺序很重要,您可以使用。

使用谓词怎么样

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)", needs];
NSArray *wants_needs = [wants filteredArrayUsingPredicate:predicate];

如果需求中包含不存在于需求中的对象,那么它不会崩溃吗?@TompaLompa不,它不会崩溃。从NSMutableArray文档中:
如果接收数组不包含otherArray中的对象,则该方法无效(尽管它会导致搜索内容的开销)。
这不是XOR,就像需要包含不需要的对象一样,结果不会包含该对象。幸运的是,该鞋也适合该对象,这是一个非常简单/光滑的解决方案。我知道必须有一条比我要做的更短的路……这绝对是一条路。我认为顺序在本例中并不重要,但即使是这样,集合对于这种行为也是高度优化的。请注意,
sensorderedset
仅在OSX中可用≥ 10.7和iOS≥ 5.0.我没有投反对票,但这个算法至少是O(n^2),应该更简单。是的,可能不是最好的方法。Hi@Gyfis,使用谓词可以减少过滤数据的一半时间。谢谢你的解决方案。