« NSCFBoolean hashes incorrectly | Main | xcodebuild drives me nuts, or is it the unix shell ? »

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

About

This page contains a single entry from the blog posted on May 6, 2006 10:10 PM.

The previous post in this blog was NSCFBoolean hashes incorrectly.

The next post in this blog is xcodebuild drives me nuts, or is it the unix shell ?.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 4.25