【问题标题】:Array value replacement from array within array in iPhoneiPhone中数组内数组的数组值替换
【发布时间】:2011-07-19 12:32:34
【问题描述】:

我有这样的数组输出。

 {
    id = 1;  
    user = {
            name="ABC"
            }
 },  
{

    id = 2;  
    user = {
            name="XYZ"
            }
 },       

我必须将 id 更改为 5 和 6,并将名称更改为“asd”和“fgh”。我的数组定义在这里。

      myArray=[[NSMutableArray alloc]initWithArray:statuses];

这是我改变“id”的方法。

 [[myArray objectAtIndex:0]replaceObjectAtIndex:0 withObject:@"5"];
 [[myArray objectAtIndex:0]replaceObjectAtIndex:0 withObject:@"6"];

但我得到了以下接受。

 [__NSCFDictionary replaceObjectAtIndex:withObject:]: unrecognized selector sent to  instance 0x6511810
 Terminating app due to uncaught    exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x6511810'

【问题讨论】:

    标签: nsmutablearray nsarray


    【解决方案1】:

    myArray 似乎是不可变的 NSArray。

    你是如何声明 myArray 的?

    编辑:

    First:
    

    myArray = [statuses mutableCopy];

    第二:

    [[myArray objectAtIndex:0] setObject:@"5" forKey:@"id"]; 
    [[myArray objectAtIndex:1] setObject:@"6" forKey:@"id"];
    [[[myArray objectAtIndex:0] objectForKey:@"user"] setObject:@"asd" forKey:@"name"];
    [[[myArray objectAtIndex:1] objectForKey:@"user"] setObject:@"fgh" forKey:@"name"]; 
    

    【讨论】:

    • 尝试一下:myArray= [statuses mutableCopy];
    • 我改了,还是一样的问题。
    • [[myArray objectAtIndex:i] setObject:@"5" forKey:@"id"]; - 这是错误的;这是你更新的方式,因为你里面有一本字典。
    • 不会把“id”改成5吗?我将如何更改“用户”内部的用户名?谢谢
    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2019-04-21
    • 2020-06-26
    相关资源
    最近更新 更多