简体   繁体   中英

NSMutableDictionary + NSMutableArray crash

I have an NSMutableDictionary called "myScheduleFullDictionary" set up like this:

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

etc.

I'm trying to parse it - basically grab one of the MutableArrays contained as the Value of one of the Keys. Here is my code:

// 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;
    }
}

The error I get is:

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

Not sure why its pointing out "name" as the "unrecognized selector." "name" is an NSString property of a "Session" class I declared and am working with - could that be related somehow?

Any insights?

EDIT:

Here is my "SessionObject" class definition:

@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
-[__NSDictionaryM name]: unrecognized selector sent to instance 0x1c5fb2d0

This means that you are trying to call name on an NSMutableDictionary where as you should have called it on an object of class SessionObject . Check the line where you are calling something like myObject.name or [myObject name] and see if myObject is of type SessionObject and not NSMutableDictionary .

Here __NSDictionaryM denotes the NSMutableDictionary type.

I'm not sure where your bug comes from - but what are you doing there? Why don't you just write

sessionsInThatDayArray = [myScheduleFullDictionary objectForKey:targetDayString];

??? That's what NSDictionary is there for - you don't search for things by hand, you just call the method to look up the key. Instead you copied the dictionary, you extracted all the keys, you sorted the keys, you iterated through them one by one until you found it - and then you called objectForKey!!!

Apart from that, in the debugger set a breakpoint on all Objective-C exceptions. It will stop when the offending code is called, so no need to search for a needle in the haystack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM