开发者

Objective-C throws NSInvalidArgumentException with Dictionaries & Arrays

I am attempting to follow a tutorial and implement it in my own code.

Here's the problem I'm getting -

data, wishlists, and wishlistids are all NSMutableArrays. I know this problem is occuring on the line where I try to add the dictionary to the data array. Look at the code:

- (void)setupWishlists:(NSString *)informationReturned
{
    if(![informationReturned isEqualToString:@""]){
        //[data setArray:[informationReturned componentsSeparatedByString:@"."]]; //this works too, its an old method I kept just incase... this one has the raw combined wishlists name and id together. Here I attempt to split them.

        NSMutableArray *temporaryArray = [NSMutableArray array];
        NSArray *rawArray = [informationReturned componentsSeparatedByString:@"."];
        for (NSString *item in rawArray) {
            //so pretty much here we have the raw information that came back... remove the IDs
            [temporaryArray setArray:[item componentsSeparatedByString:@","]];
            [wishlists addObject:[temporaryArray objectAtIndex开发者_如何学编程:0]];
            [wishlistids addObject:[temporaryArray objectAtIndex:1]];
        }
        //Initialize the array.
        NSDictionary *myWishlistsDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithArray:wishlists] forKey:@"My Wishlists"];

        [data addObject:myWishlistsDict]; //GETTING PROBLEM HERE!!! IF I COMMENT THIS LINE IT IS FINE   
    }
    NSLog(@"Raw Wishlists Array: %@", [data description]);
}

Also I am sure to alloc all of those arrays etc in a function I am sure to run before this one.

data = [[NSMutableArray alloc] init];   
wishlists = [[NSMutableArray alloc] init];
wishlistids = [[NSMutableArray alloc] init];

This is the error(seen in console:

2010-08-26 19:53:47.598 LoginApp[7604:207] -[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x59b5760
2010-08-26 19:53:47.600 LoginApp[7604:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance 0x59b5760'

Anyway I feel like I left something out... If I did please tell me and I will be sure to answer quickly.


From the code that you have provided, everything appears to be correct. The problem likely lays elsewhere.

Usually errors where the selector was sent to the wrong instance happen because the original object has been deallocated and a new instance of a different class has been allocated in its spot; all the while something still has a reference to the old deallocated instance (in this case, something somewhere still thinks an NSString lives at address 0x59b5760, but the string has been deallocated and an NSDictionary has been allocated at the same address).

If you use NSZombieEnabled the runtime will replace deallocated objects with a zombie objects, and can give you a better clue as to where the memory mishap has occurred.

Carefully revise other parts of your code to make sure that all of your code follows the memory management rules. Sometimes, it only takes a single over-release to cause a massive headache.


Your comment about where you are getting the error is not completely correct. Adding a dictionary to an array will not cause an unrecognised selector exception.

You need to run the code in the debugger with the break on Objective-C exceptions option set. That will give you the line of code at which the exception happens.

The cause is either an over release somewhere, in which case, dreamlax's answer should help you find the problem, or you are simply taking an object out of the data array and assuming it is a string when it isn't.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜