简体   繁体   English

Objective-C:添加到静态NSDictionary时,它将引发NSInvalidArgumentException

[英]Objective-C: Static NSDictionary throws NSInvalidArgumentException when I add to it

I have a method that is supposed to take an NSManagedObject, copy its attributes into a dictionary, then add the dictionary to an NSMutableArray in a static NSMutableDictionary with NSManagedObjectID keys. 我有一种方法,应该采用NSManagedObject,将其属性复制到字典中,然后将字典添加到具有NSManagedObjectID键的静态NSMutableDictionary中的NSMutableArray。 The problem is that it crashes when I try to add to a static NSMutableDictionary and only works if I make one on the spot. 问题是,当我尝试添加到静态NSMutableDictionary时,它崩溃了,并且仅当我现场制作一个时才起作用。

The problem is definitely related to the static NSMutableDictionary changes because I do not get the exception if I use a non-static dictionary. 该问题肯定与静态NSMutableDictionary更改有关,因为如果我使用非静态字典,则不会得到异常。 It's defined like this (above @implementation): 定义如下(在@implementation上方):

static NSMutableDictionary* changes = nil;

And here is the method: 这是方法:

+ (void)acceptChange: (NSManagedObject *)change{
if (!changes){
    NSLog(@"Making new changes dicitonary"); //it prints this when I run
    changes = [[NSDictionary alloc] init];
}
NSManagedObjectID* objectID = change.objectID;
NSMutableArray* changeArray = [changes objectForKey: objectID];
bool arrayDidNotExist = NO;
if (!changeArray){
    changeArray = [[NSMutableArray alloc] init];
    arrayDidNotExist = YES;
}
[changeArray addObject: [(this class's name) copyEventDictionary: change]]; //copies the NSManagedObject's attributes to an NSDictionary, assumedly works
if (arrayDidNotExist) [changes setObject: changeArray forKey: objectID];//throws the  exception

//If I do the exact same line as above but do it to an [[NSMutableDictionary alloc] init] instead of the static dictionary changes, it does not throw an exception.

if (arrayDidNotExist) NSLog(@"New array created");
NSLog(@"changeArray count: %d", changeArray.count);
NSLog(@"changes dictionary count: %d", changes.count);

} }

The exact exception message is this: 确切的异常消息是这样的:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0xa788e30'

Use NSMutableDictionary instead of NSDictionary . 使用NSMutableDictionary而不是NSDictionary You are getting exception as because , NSMutableDictionary can be modified dynamically, NSDictionary cannot. 您正在获得例外,因为, NSMutableDictionary可以动态修改,而NSDictionary无法。 .

NSMutableDictionary is subclass of NSDictionary . NSMutableDictionaryNSDictionary子类。 So all methods of NSDictionary is accessible via NSMutableDictionary object. 因此,可以通过NSMutableDictionary对象访问NSDictionary所有方法。 Moreover NSMutableDictionary also adds complementary methods to modify things dynamically, such as the method setObject:forKey: 此外, NSMutableDictionary还添加了补充方法来动态修改事物,例如setObject:forKey:方法setObject:forKey:

EDIT 编辑

You have initialized it using NSDictionary instead of `NSMutableDictionary. 您已使用NSDictionary而不是`NSMutableDictionary对其进行了初始化。

if (!changes){
    NSLog(@"Making new changes dicitonary"); //it prints this when I run
    //changes = [[NSDictionary alloc] init]; 
                ^^^^^^^^^^^^^^ ------------------> Change this. 
    changes = [[NSMutableDictionary alloc] init];
}

[__NSDictionaryI setObject:forKey:] shows that your dictionary is immutable. [__NSDictionaryI setObject:forKey:]显示您的字典是不可变的。 You are actually initializing your dictionary as immutable. 您实际上是将字典初始化为不可变的。 That's why its raising exception on adding an object. 这就是为什么它在添加对象时引发异常的原因。

Here change this line: 在这里更改此行:

if (!changes){
   ....
    changes = [[NSDictionary alloc] init];
}

to: 至:

if (!changes){
    ....
    changes = [[NSMutableDictionary alloc] init];
}

You declared your dictionary to be of NSMutableDictionary, so at compile time your dictionary is of NSMutable dictionary, but at run time it is NSDictionary as you allocated it as NSDictionary, to which you can not make changes, hence the exception. 您已将字典声明为NSMutableDictionary,因此在编译时,该字典为NSMutable字典,但是在运行时,它是NSDictionary,因为您将其分配为NSDictionary,无法对其进行更改,因此是例外。 Please define the dictionary as :- 请将字典定义为:-

changes = [[NSMutableDictionary alloc] init]; 更改= [[NSMutableDictionary分配]初始化];

If you read the description of your exception, it says the same thing. 如果您阅读了有关异常的说明,则说明的是同一件事。

Hope this helps. 希望这可以帮助。

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

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