NSDictionaries的NSDictionary

时间:2011-11-07 01:40:11

标签: objective-c ios arrays xcode nsdictionary

我在使用以下代码时遇到问题,无法创建NSDictionaries的NSDictionary。没有编译错误,但在运行时,它在此代码上失败。

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
                         ];

这是我的第一个应用程序,因此非常感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:9)

你的钥匙也必须是物品。 (除了值之外。)您使用01 int类型作为键。您应该使用以下内容:

[NSNumber numberWithInt:0];

因此,您的字典代码如下所示:

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
                         ];

答案 1 :(得分:0)

使用文字:

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

在您的代码中:

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
                     ];