NSDictionary文字,所有值方法的异常



给定以下接口:

@interface DetailViewController ()
@property (nonatomic, strong) NSDictionary *contentTypeToString;
@property (nonatomic, strong) NSDictionary *contentTypeToContent;
@property (nonatomic, strong) NSArray *contentDestcriptors;
@property (nonatomic, strong) NSArray *content;
@end

以及以下方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //dumb data
    self.contentTypeToString  =@{
    [NSNumber numberWithInt:ContextTypePlace]   : @"Place",
    [NSNumber numberWithInt:ContextTypeDate]    : @"Date",
    [NSNumber numberWithInt:ContextTypeTime]    : @"Time",
    [NSNumber numberWithInt:ContextTypeWeather] : @"Weather",
    [NSNumber numberWithInt:ContextTypeSeason]  : @"Season",
    [NSNumber numberWithInt:ContextTypeTimeOfDay] : @"Time of Day",
    [NSNumber numberWithInt:ContextTypePlace]   : @"Place",
    };
    self.contentTypeToContent = @{
    [NSNumber numberWithInt:ContextTypePlace]   : @"Dublin",
    [NSNumber numberWithInt:ContextTypeDate]    : @"21.12.2012",
    [NSNumber numberWithInt:ContextTypeTime]    : @"21:32",
    [NSNumber numberWithInt:ContextTypeWeather] : @"Cloudy",
    [NSNumber numberWithInt:ContextTypeSeason]  : @"Winter",
    [NSNumber numberWithInt:ContextTypeTimeOfDay] : @"Evening",
    [NSNumber numberWithInt:ContextTypePerson]  : @"John, Ann",
    };
    self.contentDestcriptors = [self.contentTypeToString allValues];
    self.content = [self.contentTypeToContent allValues];
}

我得到:

由于未捕获的异常NSRangeException而终止应用程序,原因:*** -[__NSArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 5]

如果我注释掉我使用allValues方法的地方,代码运行良好。文字错误,还是我的错?

您指定ContextTypePlace两次

这将创建一个包含6项而不是7项的词典。

最新更新