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

问题描述:

我有一个包含许多路径对象的NSMutableArray路径。如何知道一个NSMutableArray是否包含一个对象?

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

我试着用:

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

,但它不工作。

所以,我也试图与谓词:

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

但我有一个错误,控制台说我路径不是一个集合。

编辑:

我的路径数组是:

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; 
    } 

但这种方法不工作过。

+0

尝试定义与块的谓语。它可以帮助。 – holex 2013-04-04 16:07:49

+2

什么是Path对象?它是一个NSIndexPath?你的最初想法对我来说很合适,这取决于aPath如何实现isEqual: – danh 2013-04-04 16:28:54

EDITED(在岗新信息上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 
} 

OR

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 
} 
+0

不,你的2个解决方案对我来说不适用。我用我的数据编辑我的帖子。 – cmii 2013-04-04 19:03:29

+0

比较数组项目的规则是什么?我只比较指针,因为条件没有很好地定义; **这就是我评论相关路线**的原因。您必须用正确的比较代码替换这些行。 – holex 2013-04-04 21:23:00

  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]; 
    } 
    

欲了解更多信息,请参阅Implementing Equality and Hashing

+0

Equals重命名为isEqual – 2013-04-04 19:44:44

+0

我使用您的建议编辑我的帖子。我想念什么?因为它不起作用。 – cmii 2013-04-04 20:04:12

+0

尝试与'isEqualToArray:'比较,''isEqualToDictionary:'如果它们是数组,字典......如果您在'isEqual:'方法中记录属性以查看哪些内容没有返回true – 2013-04-04 20:21:49