• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: hash problem with NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hash problem with NSString


  • Subject: Re: hash problem with NSString
  • From: Eric Ocean <email@hidden>
  • Date: Mon, 27 Sep 2004 13:05:27 -0700

Okay, here is code that demonstrates the problem *(might not really be a problem though--see below):

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


const char char_array[5] = { 'R', 'o', 'o', 't', '\0' };
NSString *stringKey = [NSString stringWithCString:&char_array length:5];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];


BOOL equal = [stringKey isEqualToString:@"Root"];


if (equal) NSLog(@"strings are equal");
else NSLog(@"strings aren't equal");


[dict setObject:[NSArray array] forKey:stringKey];


NSArray *result = [dict objectForKey:@"Root"];


NSLog(@"result is %@", result);
NSLog(@"dict is %@", dict);


[pool release];
return 0;
}

Here is the output on the console:

[Session started at 2004-09-27 12:59:01 -0700.]
2004-09-27 12:59:00.988 NSStringHashBug[6309] strings are equal
2004-09-27 12:59:00.989 NSStringHashBug[6309] result is (null)
2004-09-27 12:59:00.989 NSStringHashBug[6309] dict is <CFDictionary 0x306ad0 [0xa01900e0]>{type = mutable, count = 1, capacity = 4, pairs = (
4 : <CFString 0x300de0 [0xa01900e0]>{contents = "Root

Executable “NSStringHashBug” has exited with status 0.

------

Of course, you see the problem: I'm using -[NSString stringWithCString: length:] incorrectly, because I included the trailing null byte in the length, when I shouldn't have. Using either

NSString *stringKey = [NSString stringWithCString:&char_array length:4];
or
NSString *stringKey = [NSString stringWithCString:&char_array];

gives this output from the Console (as expected):

[Session started at 2004-09-27 13:02:51 -0700.]
2004-09-27 13:02:50.942 NSStringHashBug[6315] strings are equal
2004-09-27 13:02:50.943 NSStringHashBug[6315] result is <CFArray 0x300a30 [0xa01900e0]>{type = immutable, count = 0, values = (
)}
2004-09-27 13:02:50.943 NSStringHashBug[6315] dict is <CFDictionary 0x306ad0 [0xa01900e0]>{type = mutable, count = 1, capacity = 4, pairs = (
0 : <CFString 0x300de0 [0xa01900e0]>{contents = "Root"} = <CFArray 0x300a30 [0xa01900e0]>{type = immutable, count = 0, values = (
)}
)}

Executable “NSStringHashBug” has exited with status 0.

-----

I haven't checked yet, but I think this will also solve another mysterious bug with NSLog() (which I asked about on xcode-users).

Regards,

Eric Ocean
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: hash problem with NSString
      • From: Nat! <email@hidden>
References: 
 >hash problem with NSString (From: Eric Ocean <email@hidden>)

  • Prev by Date: Programatically create a NSRadioButton
  • Next by Date: Advanced?? Bindings Questions
  • Previous by thread: hash problem with NSString
  • Next by thread: Re: hash problem with NSString
  • Index(es):
    • Date
    • Thread