how to init and get a dictionary working

kensuguro
Unregistered
 
Post: #1
I'm having a hard time getting a working dictionary for some reason. I have a class that holds a lookup table, so if I do class(5), it'll throw me back say, "bongos". It can just be 2 arrays, but since I don't know how to use a dictionary, I'm doing this to learn.

In terms of syntax, I have a
Code:
    NSDictionary *codeList;
declaration in the .h file and a
Code:
NSDictionary *codeList = [NSDictionary dictionaryWithObjectsAndKeys:
    @"Cherry Coke",      [NSNumber numberWithInt:1],
    @"Pizza",            [NSNumber numberWithInt:2],
    @"Meat Balls",       [NSNumber numberWithInt:3],
    @"Wiener Schnitzel", [NSNumber numberWithInt:4], nil];
in the implementation. The implmentation is not a part of any method, it's just there.

The compiler returns a "initializer element is not constant" error message. I'm puzzled because the syntax seems right. Perhaps I need to put the initializing code as a class specific init? Or is it some other problem?

I got Cocoa with Objective C, Objective C Pocket Reference, and Cocoa Programming but they all pretty much zoom past the collections, so I'm frankly quite lost. Sometimes it's the basics that's the hardest because everyone assumes you know it already.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
kensuguro Wrote:Perhaps I need to put the initializing code as a class specific init?

Precisely. Since your initialization involves method calls, rather than just assigning a constant value to the variable, it needs to be inside a function.

- Alex Diener
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #3
Just stick it in your -(id)init{} method Smile
Quote this message in a reply
kensuguro
Unregistered
 
Post: #4
gotcha, I did the init thing right after I posted the question and was happy it worked, and also was the right answer. The whole dictionary thing now works. The program kinda sucks tho.. I input 1, and press a button, the program returns "apple". Way cool...

By the way, is there any way to reverse the lookup? Like, have the dictionary return Apple when I input 1, and also return 1 when I input Apple? (other than making double entries)
Quote this message in a reply
Member
Posts: 196
Joined: 2003.10
Post: #5
Sure - you'll have to iterate through your dictionary comparing each value against the input, though.

Use an NSEnumerator to iterate through your nsdictionary. It looks complicated, but it's actually quite simple. Apple's docs are good for this one.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Dictionary Checking for word games avidgamer101 5 4,441 Jan 14, 2013 02:28 AM
Last Post: avidgamer101
  Newbie Cocoa query - setTitle: at window init sealfin 3 5,043 Nov 14, 2005 10:32 AM
Last Post: akb825
  Dictionary vs Class Justin Brimm 3 3,155 Feb 5, 2003 04:56 PM
Last Post: Justin Brimm
  NSOpenGLView's init not being called... why? codemattic 3 4,787 Aug 12, 2002 10:59 PM
Last Post: codemattic