Novarg
Novarg

Reputation: 7440

NSMutableDictionary doesn't initialize correctly

I've encountered a strage error today. I have an NSArray and an NSMutableDictionary. The problem is that my NSMutableDictionary becomes an array. Here's the code:

header:

NSArray *tableViewCellValues;
NSMutableDictionary *inputValues;

implementation:

- (void)viewDidLoad
{
  [super viewDidLoad];
  tableViewCellValues = [NSArray arrayWithObjects:@"First", @"Second", @"Third", nil];
  inputValues = [NSMutableDictionary dictionary];
  //other code...
}

So normally I should get an array with "First", "Second" and "Third" as objects and an empty NSMutableDictionary. When I print it in the log right after initializing NSMutableDictionary I get the following:

(lldb) po tableViewCellValues
(NSArray *) $1 = 0x00000000 <nil>
(lldb) po inputValues
(NSMutableDictionary *) $2 = 0x06a80420 <__NSArrayI 0x6a80420>(
First,
Second,
Third
)

(lldb) po [tableViewCellValues class]
error: Couldn't execute function; result was eExecutionDiscarded
(lldb) po [inputValues class]
(id) $4 = 0x01453b64 __NSArrayI

I'm kinda confused there. I tried cleaning and running it again, but nothing happens.

I'm using Mac OS X 10.7.3 and Xcode 4.3 if it's important.

Any help would be appreciated.

Upvotes: 1

Views: 1456

Answers (3)

BootMaker
BootMaker

Reputation: 1647

The new literal syntax is even better:

self.myMutDict = [@{} mutableCopy]

or the longer version without property just for understanding:

NSDictionary *emptyDict = @{};
NSMutableDictionary *myMutDict = [emptyDict mutableCopy];

Upvotes: 1

Rajneesh071
Rajneesh071

Reputation: 31073

This is because you are not allocate your dictionary

use this code

inputValues = [[NSMutableDictionary alloc] init];

Upvotes: -1

Brian Driscoll
Brian Driscoll

Reputation: 19635

You really want to allocate memory for your dictionary, so it should be:

self.inputValues = [[NSMutableDictionary alloc] init];

Upvotes: 2

Related Questions