Reputation: 3636
I have a NSMutableArray Paths which contains many Path objects.
I would know if a specific path is in Paths.
I tried with :
if([paths containsObject:aPath]) {
return YES;
}
but it doesn't work.
So, I tried also with Predicates :
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@", path];
NSArray *filteredArray = [self.paths filteredArrayUsingPredicate:predicate];
But I have an error, console say me that Paths is not a collection.
EDIT :
My array of paths is :
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)"
)
A path is :
PATH: (
2,
2,
2,
5,
5,
5,
6,
5,
4
)
EDIT 2 :
I add in 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];
}
and in my controller :
if([paths containsObject:aPath]) {
return YES;
}
But this method doesn't work too.
Upvotes: 1
Views: 145
Reputation: 24041
EDITED (for the new info in the post on 04013)
I would try something like this, if I were you:
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
}
Upvotes: 0
Reputation: 45551
Your first instinct is right, use containsObject:
if([paths containsObject:aPath]) {
return YES;
}
But, that didn't work for you because you're using a custom subclass and haven't implemented (I'm assuming) isEqual:
. I don't know the path properties, so you'll have compare any instance variables path contains.
- (BOOL)isEqual:(id)other
{
return ([other isKindOfClass:[Path class]] &&
// TODO: instance variable checks like...
// [[other ivar] isEqual:_ivar] &&
// [[other ivar2] isEqual:_ivar2]
}
Finally, according to the docs, if you implement isEqual:
you must also implement hash
- (NSUInteger)hash
{
return [_ivar hash] ^ [_ivar2 hash];
}
For more information see Implementing Equality and Hashing
Upvotes: 2