ios - 应用程序在 [__NSCFDictionary setObject :forKey:] in iOS 9 only 处崩溃

标签 ios objective-c ios9

在验证空值时,崩溃

-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object

当我使用所有可变类型时。(仅在 iOS 9 中崩溃,Appstore 中我的应用程序的其他版本工作正常)

谁能建议我如何在 null 条件下为键设置值。

 NSMutableArray *tempelementsArray=[[NSMutableArray alloc]init];

 if(![[catDic objectForKey:@"menuElementList"] isEqual:@""]){
                    tempelementsArray   =  [catDic objectForKey:@"menuElementList"];

    if(tempelementsArray != nil && [tempelementsArray count]>0)
      {
        for (NSInteger j=0; j<tempelementsArray.count; j++) {
        NSMutableDictionary *elementDic  = [[NSMutableDictionary alloc]init];
        elementDic = [tempelementsArray objectAtIndex:j];

       [elementDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    if ([obj isKindOfClass:[NSNull class]])
      {
        [elementDic setValue:@"" forKey:key];//App crashes here when one of the value is NULL
      }
     } ];

 }  

发生以下崩溃:

*** Terminating app due to uncaught exception NSInternalInconsistencyException', reason: '-[__NSCFDictionary  setObject:forKey:]: mutating method sent to immutable object'
*** First throw call stack:

(
0   CoreFoundation                      0x00df3a94 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x0051be02 objc_exception_throw + 50
2   CoreFoundation                      0x00df39bd +[NSException raise:format:] + 141
3   CoreFoundation                      0x00d0ed68 -[__NSCFDictionary setObject:forKey:] + 104
4   Foundation                          0x001051ba -[NSMutableDictionary(NSKeyValueCoding) setValue:forKey:] + 68
5   coreDataMenuSample                  0x000481d9 __33-[ViewController SaveToCoredata:]_block_invoke188 + 217
6   CoreFoundation                      0x00df2849 ____NSDictionaryEnumerate_block_invoke417 + 41
7   CoreFoundation                      0x00cd5452 CFBasicHashApply + 130
8   CoreFoundation                      0x00d12481 __NSDictionaryEnumerate + 273
9   CoreFoundation                      0x00d122ed -[NSDictionary enumerateKeysAndObjectsWithOptions:usingBlock:] + 45
10  CoreFoundation                      0x00d12235 -[NSDictionary enumerateKeysAndObjectsUsingBlock:] + 53
11  coreDataMenuSample                  0x00043e71 -[ViewController SaveToCoredata:] + 6481
12  coreDataMenuSample                  0x0004239d -[ViewController viewDidLoad] + 893
13  UIKit                               0x0133fd74 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 44
14  UIKit                               0x013448c2 -[UIViewController loadViewIfRequired] + 1556
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我什至检查过这个类似的问题Strange “mutating method sent to immutable object” error when adding an object to a mutable array

Saving CLLocation error: Mutating method sent to immutable object

最佳答案

看你的代码,我觉得是这一行有问题

NSMutableDictionary *elementDic  = [[NSMutableDictionary alloc]init];
        elementDic = [tempelementsArray objectAtIndex:j];

tempelementsArray 包含 NSDictionary 而不是 NSMutableDictionary 的实例。更改此代码将有助于:

NSMutableDictionary *elementDic  = [[NSMutableDictionary alloc]initWithDictionary:tempelementsArray[j]];

关于ios - 应用程序在 [__NSCFDictionary setObject :forKey:] in iOS 9 only 处崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33365064/

相关文章:

ios - 在自定义单元格(xib)中滚动时,UITextField中的文本消失

iphone - 代码行内存泄漏

ios - 验证 podspec 时出错

swift - 需要帮助将数组内容从 PFFile 转换为 NSData,然后转移到应用程序扩展

iOS:在文本字段之间切换的自动方式

ios - 使用 Facebook 应用程序时 Facebook 登录回调不起作用 - Swift

objective-c - NSTask/Process 弃用的方法和属性

iphone - 如何更改 iphone 中标题的字体大小?

ios - 为什么有这么多canOpenURL?

iphone - 自定义 "Loading..."UIView