1

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

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

我试过:

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

但它不起作用。

所以,我也尝试了 Predicates :

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

但是这种方法也行不通。

4

2 回答 2

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

有关更多信息,请参阅实现平等和散列

于 2013-04-04T19:31:41.080 回答
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
}
于 2013-04-04T16:14:35.737 回答