Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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 xcode更新NSMutableArray_Ios_Xcode_Nsmutablearray - Fatal编程技术网

Ios xcode更新NSMutableArray

Ios xcode更新NSMutableArray,ios,xcode,nsmutablearray,Ios,Xcode,Nsmutablearray,我有一个全局NSMutableArray,需要用值更新它。NSMutableArray在.h中定义如下: @property (strong, nonatomic) NSMutableArray *myDetails; 在viewDidLoad中,像这样预填充 NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"rowNumber", @"125", @"yards", nil];

我有一个全局NSMutableArray,需要用值更新它。NSMutableArray在.h中定义如下:

@property (strong, nonatomic) NSMutableArray *myDetails;
在viewDidLoad中,像这样预填充

    NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"rowNumber", @"125", @"yards", nil];
    NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"2", @"rowNumber", @"325", @"yards", nil];
    NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"3", @"rowNumber", @"525", @"yards", nil];
self.myDetails = [[NSMutableArray alloc] initWithObjects:row1, row2, row3, nil];
然后,当用户更改文本字段时,此代码将在此运行

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSObject *rowData = [self.myDetails objectAtIndex:selectedRow];

    NSString *yards = textField.text;

    [rowData setValue:yards forKey:@"yards"];

    [self.myDetails replaceObjectAtIndex:selectedRow withObject:rowData];
}
在[rowData setValue:yards forKey:@“yards”]行上单步执行代码时;它返回这个错误

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

数组是可变的,但其中包含的内容。。。NSDictionary。。。事实并非如此。你从数组中抓取一个对象

NSObject *rowData = [self.myDetails objectAtIndex:selectedRow];
然后你试着改变那个物体

[rowData setValue:yards forKey:@"yards"];

数组中的对象就是您正在更改的对象。。。它是字典,不可变的,你不能改变它。如果希望字典是可变的,则必须使用NSMutableDictionary

Jody是正确的,但是:您都在试图修改数组中已经存在的字典,并且“替换”字典。我把“替换”放在引号里,因为你用它自己替换它。您可以使用可变字典并放弃对
-replaceObjectAtIndex:withObject:
的调用,也可以继续在数组中使用不可变字典,但创建一个新字典并保留替换逻辑。谢谢你们两位,我希望不要再这样做了!