NSMutableDictionary + NSMutableArray崩溃

时间:2013-02-12 00:33:25

标签: iphone objective-c nsmutablearray nsmutabledictionary

我有一个名为“myScheduleFullDictionary”的NSMutableDictionary设置如下:

  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”指向“无法识别的选择器”。 “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

2 个答案:

答案 0 :(得分:1)

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

这意味着您正试图在name上调用NSMutableDictionary,因为您应该在类SessionObject的对象上调用它。检查您呼叫myObject.name[myObject name]的行,并查看myObjectSessionObject类型而不是NSMutableDictionary

此处__NSDictionaryM表示NSMutableDictionary类型。

答案 1 :(得分:0)

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

sessionsInThatDayArray = [myScheduleFullDictionary objectForKey:targetDayString];

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

除此之外,在调试器中为所有Objective-C异常设置断点。它会在调用违规代码时停止,因此无需在大海捞针中搜索针。