I want to have a simple NSSet which is loaded with some NSNumbers and then find out if those numbers are already added in the set or not. When I do this:
NSMutableSet *set = [[NSMutableSet alloc] init];
NSNumber *num1 = [NSNumber numberWithInt:5];
NSNumber *num2 = [NSNumber numberWithInt:5];
[set addObject:num1];
if([set member:num2]){
// something...
}
The problem is that the member always returns nil (if is false), even if those numbers are same. isEqual method returns true. So
if([num1 isEqual:num2]){
// correct
}
works...
In docs I read that member method uses isEqual so I don't know what the problem is...
Thanks for any advice.
The problem is, that you are checking if num1
and num2
are the same object. And they are not. They just have the same value but they are two different object with the same value.
So what you are checking with member
is whether they have the same address in memory.
Edit: You should use compare
to check if the values of the numbers are the same!