0

グローバルな 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'
4

1 に答える 1

2

配列は変更可能ですが、その内容は... NSDictionary... ではありません。配列からオブジェクトを取得します...

NSObject *rowData = [self.myDetails objectAtIndex:selectedRow];

そして、そのオブジェクトを変異させようとします...

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

配列内のオブジェクトはあなたが変更しているものです...そしてそれはNSDictionaryであり、不変であり、変更することはできません. 辞書を変更可能にしたい場合は、 NSMutableDictionary を使用する必要があります

于 2012-05-11T02:46:55.000 に答える