简体   繁体   English

Objective-C使用字典和数组引发NSInvalidArgumentException

[英]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. 数据,愿望清单和愿望清单都是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). 始终有一些东西引用旧的已释放实例(在这种情况下,某处仍然认为NSString位于地址0x59b5760,但是该字符串已被释放,并且NSDictionary已分配在同一地址)。

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. 如果使用NSZombieEnabled则运行时将使用僵尸对象替换已释放的对象,并且可以为您提供有关发生内存事故的更好线索。

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. 您需要在“调试器”中设置“打破Objective-C异常”选项的情况下运行代码。 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. 原因可能是某个地方的释放过度,在这种情况下,dreamlax的答案应该可以帮助您找到问题,或者您只是从数据数组中取出一个对象,并假设它不是字符串则为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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