Re: NSMutableDictionary Q's
Re: NSMutableDictionary Q's
- Subject: Re: NSMutableDictionary Q's
- From: Dietrich Epp <email@hidden>
- Date: Thu, 2 Jan 2003 16:05:04 -0800
On Thursday, January 2, 2003, at 02:12 , Mike McCune wrote:
hey all, I'm writing a small app in my further studies of cocoa and I'm
having a problem using NSMutableDictionary. I'm curious if I can use a
member of an object to be added to the dictionary as the key. here's a
small example, SomeDataClass has 2 members both NSString *'s.
SomeDataClass *ptemp;
SomeDataClass temp;
This should be *temp, right?
[temp setName: @"foo"];
[temp setDesc: @"some object"];
[SomeMutableDictionary setObject: temp forKey: [temp name]];
ptemp = [SomeMutableDictionary objectForKey: @"foo"];
now when I do this and then try to access [ptemp desc] there's nothing
there. [ptemp name] is valid, but nothing else. any ideas here? just to
clear things up, in SomeDataClass the "set" methods retain the new
NSString passed to them.
To me, this code makes very little sense. Try:
SomeDataClass* objectToAdd = [[[SomeDataClass alloc] init] autorelease];
SomeDataClass* objectReturned;
[objectToAdd setName:@"foo"];
[objectToAdd setDesc:@"some object"];
[SomeMutableDictionary setObject:objectToAdd forKey:[objectToAdd name]];
objectReturned = [SomeMutableDictionary objectForKey:@"foo"];
In addition, you probably shouldn't be retaining the NSStrings passed to
the set... methods. You should be copying them, or at least in the case
of setName.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.