Skip to content
A utility to generate a hash for NSObject subclasses.
Objective-C Ruby
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
HashBuilder
HashBuilderLib
.gitignore
.travis.yml
CHANGELOG.md
HashBuilder.podspec
LICENSE.txt
README.md

README.md

HashBuilder

Build Status Version Platform License

Used to build a hash result from contributed objects or hashes (presumably properties on your object which should be considered in the isEqual: override). The intention is for the hash result to be returned from an override to the NSObject - (NSUInteger)hash method.

Installing

If you're using CocoPods it's as simple as adding this to your Podfile:

pod 'HashBuilder', '~> 1.0'

otherwise, simply add the contents of the HashBuilder subdirectory to your project.

Documentation

To use, create a HashBuilder object, contribute to it, then query the 'builtHash' property for the resulting hash.

- (NSUInteger)hash
{
    HashBuilder *builder = [HashBuilder builder];

    [builder contributeObject:self.objectID];
    [builder contributeObject:self.occurredDate];
    [builder contributeObject:self.type];
    [builder contributeObject:self.objectURL];
    [builder contributeObject:self.tags];
    [builder contributeObject:self.count];

    NSUInteger retVal = builder.builtHash;

    return retVal;
}

It is prudent to consider the same properties when overriding your - (BOOL)isEqual:(id)object method as well.

NOTE: The order of contribution will change the resulting hash, even if all the same values are contributed. For example:

HashBuilder *builder1 = [HashBuilder builder];
[builder1 contributeObject:@"a"];
[builder1 contributeObject:[NSNumber numberWithInteger:12345]];
NSUInteger hash1 = builder1.builtHash;

HashBuilder *builder2 = [HashBuilder builder];
[builder2 contributeObject:[NSNumber numberWithInteger:12345]];
[builder2 contributeObject:@"a"];
NSUInteger hash2 = builder2.builtHash;

hash1 != hash2

Disclaimer and Licence

About

A professional iOS engineer by day, my name is Levi Brown. Authoring a technical blog grokin.gs, I am reachable via:

Twitter @levigroker
EMail levigroker@gmail.com

Your constructive comments and feedback are always welcome.

Something went wrong with that request. Please try again.