Nat! bio photo

Nat!

Senior Mull

Twitter Github Twitch

Simple fix for NSCFBoolean hash problem

A simple fix for this problem is a category on NSCFBoolean. NSCFBoolean is a hidden class, so we have to define the interface ourselves, as we want to put a category on it. Because we don't access instance variables, it really doesn't matter that this may not be the correct interface as defined by Apple. (But it is, at least according to classdump.)

#import <Foundation/Foundation.h>


@interface NSCFBoolean : NSNumber // could ignore inheritance, but less warnings this way
{
}
@end


@interface NSCFBoolean ( MulleFixHash)

- (unsigned int) hash;

@end


@implementation NSCFBoolean ( MulleFixHash)

- (unsigned int) hash
{
   return( [self unsignedIntValue]);
}

@end


int main (int argc, const char * argv[])
{
   NSAutoreleasePool *pool;
   NSNumber   *a;
   NSNumber   *b;
   
   pool = [NSAutoreleasePool new];
   
   a = [NSNumber numberWithInt:0];
   b = [NSNumber numberWithBool:NO];
   
   NSLog( @"%@ a=%@ (@%p hash=$%X)",
          [a class], a, a, [a hash]);
   NSLog( @"%@ b=%@ (@%p hash=$%X)",
          [b class], b, b, [b hash]);
   NSLog( @"[a isEqual:b] == %s",
          [a isEqual:b] ? "YES" : "NO");
   
   [pool release];
   return( 0);
}
2006-05-06 22:04:49.285 NSCFBooleanHash[1115] NSCFNumber a=0 (@0x303260 hash=$0)
2006-05-06 22:04:49.285 NSCFBooleanHash[1115] NSCFBoolean b=0 (@0xa07c1964 hash=$0)
2006-05-06 22:04:49.285 NSCFBooleanHash[1115] [a isEqual:b] == YES