Im having trouble understanding why the following code displays “The First Key = (null)” in the console/terminal:
Here is the Interface:
#import <UIKit/UIKit.h>
@interface TestingViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *namesArray;
@property (nonatomic, strong) NSDictionary *myDictionary;
@end
This is the implementation:
#import "DictionaryTestViewController.h"
@implementation DictionaryTestViewController
@synthesize namesArray;
@synthesize myDictionary;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myDictionary initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
NSLog(@"The First Key = %@", [self.myDictionary objectForKey:@"key1"]);
}
@end
Thanks in advance!
You haven’t allocated your dictionary yet. It’s
nilat the moment, and sending message to nil does nothing.Change it to this instead if you’re using ARC
or this if not