Wednesday, August 4, 2010

Objective-C - dictionaries and constants

OK - learning a new language is never fun. There are always syntax issues that are just plain hard to find since they manifest themselves in such odd ways. Here's an example:

Starting out with Objective-C is a bit of a leap for someone that left C and C++ behind in the 90s. That said, it's pretty powerful, but I am still remembering syntax. I was ripping through an example from one of the books I picked up (Head First iPhone Development) and using NSDictionary (nice construct leftover from the NextStep days). The book recommended, and I agree, that using constants to define the dictionary key names is a good thing. So, I created my first from-scratch .h file in >10 years and put in my constants. Then when I use them, as in the following example (variable names changed)
nameTextField.text = [myDictionary objectForKey:CONSTANT_KEYNAME_HERE];
which generates the following error:
Expected ']' before ';' token.

WTF?

It turns out that I had declared my constants in my .h file with a semicolon at the line end. I had forgotten that #define statements don't need that. I don't understand why the compiler didn't bark at me for that, but oh well. Just another 20 minutes gone - not a huge deal. Interesting though.