2013-02-12 89 views
0

我有一个NSMutableDictionary称为“myScheduleFullDictionary”成立这样的:的NSMutableDictionary + NSMutableArray的崩溃

KEY    VALUE 
"Day 1"   An NSMutableArray of NSMutableDictionaries 
"Day 2"   An NSMutableArray of NSMutableDictionaries 
"Day 3"   An NSMutableArray of NSMutableDictionaries 

我试图解析它 - 基本抢包含作为该MutableArrays之一其中一个键的价值。 这里是我的代码:

// First I make a mutableCopy of the entire Dictionary: 
NSMutableDictionary *copyOfMyScheduleDictionary = [myScheduleFullDictionary mutableCopy]; 

// Next I grab & sort all the KEYS from it: 
NSArray *dayKeysArray = [[copyOfMyScheduleDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

// I set up an NSMutableArray to hold the MutableArray I want to grab: 
NSMutableArray *sessionsInThatDayArray = [[NSMutableArray alloc] init]; 

// Then I iterate through the KEYs and compare each to the one I'm searching for: 
for (int i = 0; i < [dayKeysArray count]; i++) { 

    NSString *currentDayKey = [dayKeysArray objectAtIndex:i];   
    if ([currentDayKey isEqualToString: targetDayString]) { 
     NSLog(@"FOUND MATCH!!!"); 

     // I log out the NSMutableArray I found - which works perfectly: 
     NSLog(@"found array is: %@", [copyOfMyScheduleDictionary objectForKey:currentDayKey]); 

     // But when I try to actually grab it, everything crashes: 
     sessionsInThatDayArray = [copyOfMyScheduleDictionary objectForKey:currentDayKey]; 
     break; 
    } 
} 

我得到的错误是:“无法识别的选择”

-[__NSDictionaryM name]: unrecognized selector sent to instance 0x1c5fb2d0 

不知道为什么它指出“名称”为“name”是我声明和正在使用的“Session”类的NSString属性 - 可能有某种关系吗?

任何见解?

编辑:

这里是我的“SessionObject”类定义:

@interface SessionObject : NSObject 


@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *speaker; 
@property (nonatomic, strong) NSString *location; 
@property (nonatomic, strong) NSDate *startTime, *endTime; 
@property (nonatomic, strong) NSString *notes; 
@property (nonatomic, strong) NSString *dayOfConference; 


@end 
+0

'name'在哪里被使用? – yeesterbunny 2013-02-12 00:44:02

+0

查看更新的问题 – sirab333 2013-02-12 00:50:56

+2

您确定这是导致错误的行吗?你有没有添加异常断点?日志应该工作没有意义,但不是下一行。 – rdelmar 2013-02-12 00:50:58

回答

1
-[__NSDictionaryM name]: unrecognized selector sent to instance 0x1c5fb2d0 

这意味着,你正在试图调用nameNSMutableDictionary哪里,你应该把它叫做上对象类SessionObject。请检查您拨打电话的地址,如myObject.name[myObject name],并查看myObject的类型是SessionObject而不是NSMutableDictionary

这里__NSDictionaryM表示NSMutableDictionary类型。

0

我不确定你的bug来自哪里 - 但你在那里做什么?你为什么不直接写

sessionsInThatDayArray = [myScheduleFullDictionary objectForKey:targetDayString]; 

???这就是NSDictionary的用处 - 你不需要手动搜索,只需调用方法来查找关键字即可。相反,您复制了字典,提取了所有密钥,对键进行了排序,逐个遍历它们直到找到它 - 然后调用了objectForKey!

除此之外,在调试器中设置所有Objective-C异常的断点。当违规代码被调用时它会停止,所以不需要在干草堆中搜索针。

相关问题