jrble819
jrble819

Reputation: 119

NSDictionary of NSDictionaries

I'm having trouble with the following code to create an NSDictionary of NSDictionaries. There is no compile error, but during runtime, it fails on this code.

NSDictionary *section0 = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSDictionary dictionaryWithObjectsAndKeys:
                                @"Caller ID", @"label",
                                @"name", @"field",
                                @"Call", @"fieldType", nil
                            ], 0,
                            [NSDictionary dictionaryWithObjectsAndKeys:
                                @"Number", @"label",
                                @"number", @"field",
                                @"Call", @"fieldType", nil
                            ], 1,
                            nil
                         ];

This is my first app, so any help you can provide is greatly appreciated.

Upvotes: 1

Views: 1508

Answers (2)

ILYA2606
ILYA2606

Reputation: 605

Use literals:

NSNumber *number = @0 ; // equal [NSNumber numberWithInt:0]

In your code:

NSDictionary *section0 = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSDictionary dictionaryWithObjectsAndKeys:
                             @"Caller ID", @"label",
                             @"name", @"field",
                             @"Call", @"fieldType", nil
                         ], @0,
                         [NSDictionary dictionaryWithObjectsAndKeys:
                             @"Number", @"label",
                             @"number", @"field",
                             @"Call", @"fieldType", nil
                         ], @1,
                         nil
                     ];

Upvotes: 0

Craig Otis
Craig Otis

Reputation: 32054

Your keys also have to be objects. (In addition to the values.) You're using 0 and 1 int types as keys. You should instead use the following:

[NSNumber numberWithInt:0];

Thus, your dictionary code would look like:

NSDictionary *section0 = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSDictionary dictionaryWithObjectsAndKeys:
                                @"Caller ID", @"label",
                                @"name", @"field",
                                @"Call", @"fieldType", nil
                            ], [NSNumber numberWithInt:0],
                            [NSDictionary dictionaryWithObjectsAndKeys:
                                @"Number", @"label",
                                @"number", @"field",
                                @"Call", @"fieldType", nil
                            ], [NSNumber numberWithInt:1],
                            nil
                         ];

Upvotes: 9

Related Questions