Wiki

Clone wiki

CSHashKit / Home

CSHashKit

CSHashKit is a simple Objective-C library that implements hash and isEqual: for every Objective-C object. Below

Example Usage

TestClass is a simple class, with two ivars: intValueIvar and identifier.

TestClass.h

#!objectivec

#import <Foundation/Foundation.h>

@interface TestClass : NSObject{
    @public
    int intValueIvar;
    NSString *identifier;
}

@property (strong, nonatomic) NSString *stringProperty;

@end
This is the implementation

TestClass.m

#import "TestClass.h"
#import "NSObject+CSHash.h"

@implementation TestClass

- (id) init{
    self = [super init];

    if (self) {

        HASHABLE(identifier);
        HASHABLE_PROPERTY(stringProperty);

    }

    return self;
}

- (NSUInteger) hash{
    return [self cs_hash];
}

- (BOOL) isEqual:(id)object{
    return [self cs_IsEqual:object];
}

@end

The HASHABLE() and HASHABLE_PROPERTY() macro tell the runtime to use the selected ivar and the selected property to calculate the hash value of the object. Moreover, identifier and stringProperty will be used to determine if two object of TestClass class are equal or not.

Updated