2013-02-12 73 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異常的斷點。當違規代碼被調用時它會停止,所以不需要在乾草堆中搜索針。