Reputation: 1317
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!
Upvotes: 0
Views: 1937
Reputation: 4131
You haven't allocated your dictionary yet. It's nil
at the moment, and sending message to nil does nothing.
Change it to this instead if you're using ARC
self.myDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
or this if not
self.myDictionary = [[[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil] autorelease];
Upvotes: 5