【问题标题】:How to know if an NSMutableArray contains an object?如何知道 NSMutableArray 是否包含对象?
【发布时间】:2013-04-04 16:01:21
【问题描述】:

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

我会知道特定路径是否在 Paths 中。

我试过了:

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

但它不起作用。

所以,我也尝试了谓词:

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

但是这个方法也行不通。

【问题讨论】:

  • 尝试用块定义谓词。它会有所帮助。
  • 什么是 Path 对象?它是 NSIndexPath 吗?你最初的想法对我来说是正确的,这取决于 aPath 如何实现 isEqual:

标签: iphone nsmutablearray nspredicate


【解决方案1】:
  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

【讨论】:

  • 我根据您的建议编辑我的帖子。我错过了什么吗?因为它不起作用。
  • 尝试与 isEqualToArray:isEqualToDictionary: 进行比较,如果它们是数组、字典...如果您在 isEqual: 方法中记录属性以查看未返回的内容,也会有所帮助
【解决方案2】:

已编辑 (关于 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
}

【讨论】:

  • 不,您的 2 个解决方案对我不起作用。我用我的数据编辑我的帖子。
  • 比较数组项的规则是什么?我只比较了指针,因为条件没有定义好; 这就是我评论相关行的原因。您必须用正确的比较代码替换这些行。
猜你喜欢
  • 1970-01-01
  • 2013-10-13
  • 2011-02-11
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多