• 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: NSMutableDictionary setObject:forKey:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableDictionary setObject:forKey:


  • Subject: Re: NSMutableDictionary setObject:forKey:
  • From: mmalcolm crawford <email@hidden>
  • Date: Sat, 11 Jun 2005 16:32:45 -0700


On Jun 11, 2005, at 4:05 PM, Theodore H. Smith wrote:

setObject:forKey: What kind of objects can the keys be?

Any object.

The docs say this: "- (id)objectForKey:(id)aKey. Returns an entry’s value given its key, or nil if no value is associated with aKey."

Which is precisely what it does.

OK. But I tested this with [NSObject alloc]init] as the key, and it didn't work.

Assuming you meant that your code looked like:

    id value = [myDictionary objectForKey:[[NSObject alloc] init]];

(a) You'll be leaking the new object that you created as the key (since you'll have no way to reference and so release it later);
(b) Of course there isn't a corresponding value, since you've just "invented" the key. There isn't a corresponding value in dictionary.


Dictionaries are key-value pairs. If you did something like the following:

id myKey = [[[NSObject alloc] init] autorelease];
id myValue = [NSArray array];
NSDictionary *myDict = [NSDictionary dictionaryWithObject:myValue forKey:myKey];


"in principle" this would work. In practice it still won't since, since as stated in the documentation, "keys must conform to the NSCopying protocol" (and NSObject does not).


It's not clear whether you have yet invested in a book on Cocoa. If you have not, I would very much encourage you to do so, and to work through it methodically from the beginning. If you have, I would (seriously) encourage you to take a day or two's break, then start again from the beginning. You appear to be making things much more difficult for yourself than is necessary.


mmalc


_______________________________________________ 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: NSMutableDictionary setObject:forKey:
      • From: Andy Lee <email@hidden>
References: 
 >NSMutableDictionary setObject:forKey: (From: "Theodore H. Smith" <email@hidden>)

  • Prev by Date: Re: IBOutlet & NSTabView
  • Next by Date: Re: NSMutableDictionary setObject:forKey:
  • Previous by thread: NSMutableDictionary setObject:forKey:
  • Next by thread: Re: NSMutableDictionary setObject:forKey:
  • Index(es):
    • Date
    • Thread