how to init and get a dictionary working
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
declaration in the .h file and a
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.
In terms of syntax, I have a
Code:
NSDictionary *codeList;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];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.
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
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)
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)
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.
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Dictionary Checking for word games | avidgamer101 | 5 | 1,577 |
Jan 14, 2013 02:28 AM Last Post: avidgamer101 |
|
| Newbie Cocoa query - setTitle: at window init | sealfin | 3 | 4,524 |
Nov 14, 2005 10:32 AM Last Post: akb825 |
|
| Dictionary vs Class | Justin Brimm | 3 | 2,540 |
Feb 5, 2003 04:56 PM Last Post: Justin Brimm |
|
| NSOpenGLView's init not being called... why? | codemattic | 3 | 3,932 |
Aug 12, 2002 10:59 PM Last Post: codemattic |
|

