Help with isEquals and hash in iphone

So, I redefine isEqualsand hashto compare user objects to remove duplicates from NSArray. The problem is that I am missing some values ​​in a list that does not contain duplicate elements, and it seems that my implementation is hasheither isEqualsincorrect. A custom object is a course object that has some type variables: idand name. I will put the code here:

- (BOOL)isEqual:(id)object {
    if ([object isKindOfClass:[Course self]]) {
        return YES;
    } 
    if(self == object){
        return YES;
    }
    else {
        return NO;
    }
}

- (unsigned)hash {

    NSString *idHash = [NSString stringWithFormat: @"%d", self._id];
    return [idHash hash];
}

Then, after querying the database, I put the values ​​in an array, and then in a set that should remove duplicate elements as follows:

NSMutableSet *noDuplicates = [[NSMutableSet alloc] initWithArray:tempResults];

You see what I'm doing wrong in the implementation of isEqualsor hash?

Many thanks.

+5
4

isEqual ( , , )

Apple Doc , , ().

-(unsigned int)hash  
{  
    return 234;//some random constant  
} 

isEqual: -

-(BOOL)isEqual:(id)otherObject  
{  
  MyClass *thisClassObj = (MyClass*)otherObject;

  *// this could be replaced by any condition statement which proves logically that the two object are same even if they are two different instances* 

return ([thisClassObj primaryKey] == [self primaryKey]);

}

: -hash Cocoa

-hash/-isEqual:/-isEqualTo...: Objective-C

-14

1. , / . , , ( , , ).

2. -, . , , . C int ..

3. isEqual: , , , , isEqual:, .

, Person name ( NSString) number ( int), , hash :

-(NSUInteger) hash
{
    return [[self name] hash] ^ [self number];
}

isEqual:

-(BOOL) isEqual: (id) rhs
{
    BOOL ret = NO;
    if ([rhs isKindOfClass: [Person class]]) // do not use [self class]
    {
        ret = [[self name] isEqualToString: [rhs name]] && [self number] == [rhs number];
    } 
    return ret;
}

, , , , , .

  • [a isEqual: b] == [b isEqual: a] a b
  • [a isEqual: b] && [b isEqual: c] [a isEqual: c] a, b, c

, isEqual: , , . , [self class] .

+11

, isEqual: , . . , , , , ,

- (BOOL)isEqual:(id)object {
    if ([object isMemberOfClass:[self class]]) {
        // test equality on all your important properties
        // return YES if they all match
    }
    return NO;
}

, int . int .

+3

" ", ". - self._id .

Objective-C , . isEqual , , . , BOOL, , . isEqual, , .

- (BOOL)isEqual:(id)object {
    BOOL result = NO;

    if ([object isKindOfClass:[self class]]) {
        result = [[self firstName] isEqualToString:[object firstName]] && 
        [[self lastName] isEqualToString:[object lastName]] &&
        [self age] == [object age];
    }

    return result;
}

NSObject:

, - .

If two objects are equal (as determined by the isEqual :) method, they must have the same hash value. This last point is especially important if you define a hash in a subclass and intend to put instances of this subclass in a collection.

- (NSUInteger)hash {
    NSUInteger result = 1;
    NSUInteger prime = 31;

    result = prime * result + [_firstName hash];
    result = prime * result + [_lastName hash];
    result = prime * result + _age;

    return result;
}

So, what defines two objects as equal is determined by the programmer and their needs. However, regardless of the equality methodology, equal objects must have equal hashes.

+2
source

All Articles