如何知道NSMutableArray是否包含对象?

时间:2013-04-04 16:01:22

标签: iphone nsmutablearray nspredicate

我有一个NSMutableArray路径,其中包含许多Path对象。

我知道路径中是否有特定的路径。

我尝试过:

if([paths containsObject:aPath]) {
    return YES;
}

但它不起作用。

所以,我也尝试过Predicates:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@", path];
NSArray *filteredArray = [self.paths filteredArrayUsingPredicate:predicate];

但是我有一个错误,控制台告诉我Paths不是一个集合。

编辑:

我的路径数组是:

2013-04-04 20:57:36.465 numbs[42781:617] paths (
    "PATH: (\n    2,\n    2,\n    2,\n    5,\n    5,\n    6,\n    5,\n    4\n)",
    "PATH: (\n    2,\n    2,\n    2,\n    5,\n    5,\n    6,\n    4,\n    5\n)",
    "PATH: (\n    2,\n    2,\n    2,\n    5,\n    5,\n    4,\n    5,\n    6\n)",
    "PATH: (\n    2,\n    2,\n    2,\n    5,\n    5,\n    4,\n    6,\n    5\n)"
)

路径是:

PATH: (
    2,
    2,
    2,
    5,
    5,
    5,
    6,
    5,
    4
)

编辑2:

我添加了Path.m

- (BOOL)isEqual:(id)other
{
    return ([other isKindOfClass:[Path class]] &&
            [[other arrayOfNode] isEqual:self.arrayOfNode] &&
            [other somme] == self.somme &&
            [[other result] isEqual:self.result] &&
            [[other resultString] isEqual:self.resultString] &&
            [[other wayArray] isEqual:self.wayArray]);
}

- (NSUInteger) hash;
{
    return somme ^ [resultString hash] ^ [wayArray hash] ^ [arrayOfNode hash] ^ [result hash];
}

并在我的控制器中:

 if([paths containsObject:aPath]) {
        return YES;
    }

但这种方法也不起作用。

2 个答案:

答案 0 :(得分:2)

  1. 您的第一直觉是对的,请使用containsObject:

    if([paths containsObject:aPath]) {
        return YES;
    }
    
  2. 但是,这对你不起作用,因为你正在使用自定义子类并且没有实现(我假设)isEqual:。我不知道路径属性,所以你将比较路径包含的任何实例变量。

    - (BOOL)isEqual:(id)other
    {
        return ([other isKindOfClass:[Path class]] &&
            // TODO: instance variable checks like... 
            // [[other ivar] isEqual:_ivar] && 
            // [[other ivar2] isEqual:_ivar2]                 
    }
    
  3. 最后,根据文档,如果您实施isEqual:,您还必须实施hash

    - (NSUInteger)hash
    {
        return [_ivar hash] ^ [_ivar2 hash];
    }
    
  4. 有关详细信息,请参阅Implementing Equality and Hashing

答案 1 :(得分:0)

已审核 (有关04013帖子中的新信息)

如果我是你,我会尝试这样的事情:

Path *_path = ...; // you object you'd like to find
NSArray *_pathsArray = ...; // the array with lots of Path objects;

if ([[_pathsArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    return ([evaluatedObject isEqual:_path]); // ... or whatever how you'd like to compare them
}]] count] > 0) {
    // it contains your _path object
} else {
    // it doesn't contain you _path object
}

Path *_path = ...; // you object you'd like to find
NSArray *_pathsArray = ...; // the array with lots of Path objects;

__block BOOL _isInsideTheArray = FALSE;
[_pathsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    *stop = _isInsideTheArray = ([obj isEqual:_path]); // ... or whatever how you'd like to compare them
}];

if (_isInsideTheArray) {
    // it contains your _path object
} else {
    // it doesn't contain you _path object
}