November 29, 2012
Enola Labs

Enola Labs creates custom strategy and products for mobile and web.

Let’s Talk

Subscribe to our blog:

I was parsing a JSON response for an iOS / iPhone app and the keys in the data all started with an upper case letter. The Objective-C class that I made to represent the data defined instance variables that followed the standard convention of lower case for the first word. So for example “ParentCategory” became “parentCategory”. I wrote some code so I didn’t have to hard code all the keys being returned and instead used the method setValuesForKeysWithDictionary:. I was getting some unexpected results and after banging my head into the desk for a good bit I finally figured out what was going on.

I wrote up some test code to confirm everything and here is the puzzle of the day for you. Given this class definition what do you think will be printed out?

@interface TestViewController : UIViewController

@property (nonatomic, strong) NSString *Category;
@property (nonatomic, strong) NSString *category;

@end

@implementation TestViewController

- (void)viewDidLoad {
[super viewDidLoad];

_Category = @"Computer";
_category = @"computer";

self.Category = @"Atomic";
self.category = @"Axis";

NSLog(@"Category:%@ category:%@", self.Category, self.category);
}

@end

The answer is:

Category:Computer category:Axis

probably not what you expected on first glance. It is solving little puzzles like this that give me a good bit of satisfaction. Instead of spoiling the fun for you, I will let you play around with the code to understand what is going on. Happy Coding!