Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 发送到不可变对象的变异方法-[\uu NSCFDictionary setObject:forKey:]_Ios_Objective C_Iphone_Exception_Nsmutabledictionary - Fatal编程技术网

Ios 发送到不可变对象的变异方法-[\uu NSCFDictionary setObject:forKey:]

Ios 发送到不可变对象的变异方法-[\uu NSCFDictionary setObject:forKey:],ios,objective-c,iphone,exception,nsmutabledictionary,Ios,Objective C,Iphone,Exception,Nsmutabledictionary,我正在尝试更新NSMutableDictionary,但我发现 发送到不可变对象的变异方法 错误。itemMutableArray是NSMutableArray的一个实例,它包含NSMutableDictionary的实例。请帮忙 for(NSMutableDictionary *dict in itemMutableArray){ for (NSString *key in [dict allKeys]) { NSString *kv = [dict valueFor

我正在尝试更新
NSMutableDictionary
,但我发现

发送到不可变对象的变异方法

错误。
itemMutableArray
NSMutableArray
的一个实例,它包含
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吗?你知道它有时会做一些与你期望的完全不同的事情吗,尤其是当你无法控制钥匙的时候

您的字典显然不是NSMutableDictionary的实例,因为否则您不会出现该错误

调试规则:你认为你知道的都是错的

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

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


顺便说一句,阅读valueForKey和setValueForKey的文档。然后阅读objectForKey和setObjectForKey的文档。在阅读了它们之后,您真的想使用valueForKey吗?你知道它有时会做一些与你期望的完全不同的事情吗,尤其是当你无法控制钥匙的时候

As@Paulw正确地说,将其强制转换为NSMutableDictionary变量不会使标准dictionary可变

试试这个:-

NSMutableDictionary *dicNew = [NSMutableDictionary new];

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

正如@Paulw正确地说,将其转换为NSMutableDictionary变量不会使标准字典可变

试试这个:-

NSMutableDictionary *dicNew = [NSMutableDictionary new];

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

好的,问题是您的itemMutableArray不包含可变字典,我认为它将是一个普通字典

你现在有两个选择

1.只需将普通字典更改为添加到itemMutableArray的可变字典,然后继续编写代码

2.使用以下代码

for(int i = 0; i < itemMutableArray.count; i++)
    {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[itemMutableArray objectAtIndex:i]];
        for (NSString *key in [dict allKeys]) {
            NSString *kv = [dict valueForKey:key];
            if([kv isEqualToString:[NSString stringWithFormat:@"%@%d",@"FIELD_",k]]) {
[dict removeObjectForKey:@"Value"];
                [dict setObject:field.text forKey:@"Value"];
            }
        }
    }
for(int i=0;i
好的,问题是您的itemMutableArray不包含可变字典,我认为它将是一个普通字典

你现在有两个选择

1.只需将普通字典更改为添加到itemMutableArray的可变字典,然后继续编写代码

2.使用以下代码

for(int i = 0; i < itemMutableArray.count; i++)
    {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[itemMutableArray objectAtIndex:i]];
        for (NSString *key in [dict allKeys]) {
            NSString *kv = [dict valueForKey:key];
            if([kv isEqualToString:[NSString stringWithFormat:@"%@%d",@"FIELD_",k]]) {
[dict removeObjectForKey:@"Value"];
                [dict setObject:field.text forKey:@"Value"];
            }
        }
    }
for(int i=0;i
该词典不是可变词典。将其强制转换为NSMutableDictionary变量不会使标准字典变为可变的。能否共享将字典添加到
itemMutableArray
的代码?该字典不是可变字典。将其强制转换为NSMutableDictionary变量不会使标准字典可变。能否共享将字典添加到
itemMutableArray
的代码?valueForKey:是一种KVC方法。它适用于任何类。valueForKey:允许您使用字符串作为属性名称来访问属性。在这个问题上。tte键只是字符串,所以我使用valueForKey.OK,所以您从问题的答案中复制了一些文本。听起来你根本不知道为什么。是的,我也研究了其他答案并支持我的答案。如果我错了,请分享这些信息。
objectForKey:
是一种
NSDictionary
方法,而且工作效率更高
valueForKey:
在这种情况下不提供任何有用的内容。valueForKey:是一种KVC方法。它适用于任何类。valueForKey:允许您使用字符串作为属性名称来访问属性。在这个问题上。tte键只是字符串,所以我使用valueForKey.OK,所以您从问题的答案中复制了一些文本。听起来你根本不知道为什么。是的,我也研究了其他答案并支持我的答案。如果我错了,请分享这些信息。
objectForKey:
是一种
NSDictionary
方法,而且工作效率更高
valueForKey:
在这种情况下没有提供任何有用的信息。@trojanfoe-抱歉。更新了答案。。只需复制表单问题并编辑即可。@trojanfoe-抱歉。更新了答案。。简单地复制表格问题并编辑。