Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A utility to generate a hash for NSObject subclasses.
Objective-C Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
HashBuilder.xcodeproj
HashBuilder
Tests
.gitignore
HashBuilder-Prefix.pch
HashBuilder.podspec
LICENSE.txt
README.md

README.md

HashBuilder

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'

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
App.net @levigroker
EMail levigroker@gmail.com

Your constructive comments and feedback are always welcome.

Something went wrong with that request. Please try again.