ios - 发送到不可变对象(immutable对象)的变异方法 - [__NSCFDictionary setObject :forKey:]

标签 ios objective-c iphone exception nsmutabledictionary

我正在尝试更新 NSMutableDictionary 但我得到了

mutating method sent to immutable object

错误。 itemMutableArrayNSMutableArray 的实例,它包含 NSMutableDictionary 的实例。请帮忙。

for(NSMutableDictionary *dict in itemMutableArray){
    for (NSString *key in [dict allKeys]) {
         NSString *kv = [dict valueForKey:key];
         if([kv isEqualToString:[NSString stringWithFormat:@"%@%d",@"FIELD_",k]]) {                                      
             [dict setValue:field.text forKey:@"Value"];
         }
    }
 }

异常:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010cb07f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010c574bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010cb07e6d +[NSException raise:format:] + 205
    3   CoreFoundation                      0x000000010ca3e8a3 -[__NSCFDictionary setObject:forKey:] + 99
    4   MobileWalletBanking                 0x0000000109e41c62 -[MyViewController validateConnectionFields] + 6178
    5   MobileWalletBanking                 0x0000000109e402f1 -[MyViewController validateInputFields] + 2065
    6   MobileWalletBanking                 0x0000000109e42291 -[MyViewController performBillPayment:] + 97
    7   UIKit                               0x000000010abdb8be -[UIApplication sendAction:to:from:forEvent:] + 75
    8   UIKit                               0x000000010ace2410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    9   UIKit                               0x000000010ace17df -[UIControl touchesEnded:withEvent:] + 522
    10  UIKit                               0x000000010af88540 _UIGestureRecognizerUpdate + 9487
    11  UIKit                               0x000000010ac20ff6 -[UIWindow _sendGesturesForEvent:] + 1041
    12  UIKit                               0x000000010ac21c23 -[UIWindow sendEvent:] + 667
    13  UIKit                               0x000000010abee9b1 -[UIApplication sendEvent:] + 246
    14  UIKit                               0x000000010abfba7d _UIApplicationHandleEventFromQueueEvent + 17370
    15  UIKit                               0x000000010abd7103 _UIApplicationHandleEventQueue + 1961
    16  CoreFoundation                      0x000000010ca3d551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x000000010ca3341d __CFRunLoopDoSources0 + 269
    18  CoreFoundation                      0x000000010ca32a54 __CFRunLoopRun + 868
    19  CoreFoundation                      0x000000010ca32486 CFRunLoopRunSpecific + 470
    20  GraphicsServices                    0x000000010e14f9f0 GSEventRunModal + 161
    21  UIKit                               0x000000010abda420 UIApplicationMain + 1282
    22  MobileWalletBanking                 0x0000000109db5733 main + 115
    23  libdyld.dylib                       0x000000010dca4145 start + 1
)

最佳答案

你的字典显然不是 NSMutableDictionary 的实例,否则你不会得到那个错误。

调试法则:你以为你知道的都是错的。

将指针声明为 NSMutableDictionary* 不会使项成为可变字典。

检查将字典添加到数组的代码,并检查添加不可变字典的位置。

顺便说一句。阅读 valueForKey 和 setValueForKey 的文档。然后阅读objectForKey和setObjectForKey的文档。看完它们,你真的要用valueForKey吗?您是否了解它有时会做一些可能根本不是您期望的事情,尤其是当您无法控制按键时?

关于ios - 发送到不可变对象(immutable对象)的变异方法 - [__NSCFDictionary setObject :forKey:],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33752998/

相关文章:

android - 关于3G的任何简单解读

iphone - 移动浏览器上的网页可以通过手机询问/获取用户位置吗?

ios - 以编程方式添加时,rightAnchor 约束不适用 - swift

iOS:将 ImageView 拉伸(stretch)到 TableView 之上?

ios - 失败 : permission_denied when setValue: or removeValue: in Firebase Database | Swift 4

ios - 无法从 sqlite 表中获取最后插入的数据

ios - 将非 ARC 转换为 ARC

android - 使用加速度计计算速度

ios - 如何知道 UIView 的当前比例?

ios - 模态 Segue 导致黑屏