在NSMutableDictionary中更新密钥

时间:2013-09-23 07:38:15

标签: objective-c nsmutabledictionary

json是带有键“消息”的NSMutableDictionary。我需要使用stringByReplacingOccurrencesOfString

“清理”该密钥中的数据

NSLog(@"json: %@", json);输出:

json: {
    conversationId = 61;
    countmessagesinconversation = 2;
    message = "Messages exist!";
    messagesinconversation =     (
                {
            message = "Hi";
            messagecreated = "June 24, 2013 16:16";
            messageid = 68;
            sentby = Thomas;
            sentbyID = 1;
            title = "Subject";
        },

                {
            message = "What's  up?";
            messagecreated = "September 22, 2013 17:00";
            messageid = 331;
            sentby = Steve;
            sentbyID = 2;
            title = "Subject";
        }
    );
    success = 1;
}

所以,这就是我想出来的,但我显然错了。

NSString *newstr = [[NSString alloc]init];

    for (newstr in [[[json objectForKey:@"messagesinconversation"]allKeys]copy]){
        newstr = [newstr stringByReplacingOccurrencesOfString:@" "
                                             withString:@""];
        [json setValue:newstr forKey:@"message"];
    }

有人可以帮我解决这个问题并解释一下,我理解这个概念吗?我知道如何用数组做这个,但我的问题是(我认为)是我不完全理解如何在字典中访问正确的键。

编辑:很抱歉我的问题不明确。如果键message中有两个空格字符,那么当我在屏幕上显示" "时,会出现这种情况。

3 个答案:

答案 0 :(得分:1)

//First get the array of message dictionaries:
NSArray * messages = [json objectForKey:@"messagesinconversation"];

//create new array for new messages
NSMutableArray * newMessages = [NSMutableArray arrayWithCapacity:messages.count];

//then iterate over all messages (they seem to be dictionaries)
for (NSDictionary * dict in messages)
{
    //create new mutable dictionary
    NSMutableDictionary * replacementDict = [NSMutableDictionary dictionaryWithDictionary:dict];

    //get the original text
    NSString * msg = [dict objectForKey:@"message"];

    //replace it as you see fit
    [replacementDict setObject:[msg stringByReplacingOccurrencesOfString:@" " withString:@""] forKey:@"message"];

    //store the new dict in new array
    [newMessages addObject:replacementDict];
}

//you are done - replace the messages in the original json dict
[json setObject:newMessages forKey:@"messagesinconversation"];

答案 1 :(得分:0)

既然要求更清楚,我会尝试答案。我尝试使用与您的问题相同的名称,并在较早的答案中提出建议。

json设为您声明的NSMUtableDictionary。然后继续前进:

json = [json mutableCopy]; // creates a mutable dictionary based on json which is immutable as result of the json serialization although declared as mutable. 

NSMutableArray *msgArray = [[json objectForKey:@"messagesinconversation"] mutableCopy];  //this fetches the array from the dictinary and creates a mutable copy of it. 
[json setValue:newstr forKey:@"messagesinconversation"];  // replace the original immutable with the mutable copy. 

for (int i = 0; i < [msgArray count]; i++) {
    NSMutableDictionary mutableInnerDict = [[msgArray objectAtIndex:i] mutableCopy]; // fetching the i-th element and replace it by a mutable copy of the dictionary within. 
    [msgArray replaceObjectAtIndex:i withObject:mutableInnerDict]; // it is now mutable and replaced within the array. 
    NSString *newString = [[[msgArray objectAtindex:i] objectForKey:@"message"]  stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];  // crates a new string with all &nbsp removed with blanks. 
    [mutableInnerDict setValue:newString forKey:@"message"];
}

关于用“”取代&amp; nbsp,这真的是你想要的吗?我问,因为您的样本数据中没有出现&amp; nbsp。或者你想要删除空白?

答案 2 :(得分:0)

在另一个可变数组中添加你的字典数组

NSMutableArray *msgArray = [[json objectForKey:@"messagesinconversation"] mutableCopy];

使用for loop进行访问。

for (int i = 0 ; i < msgArray.count ; i++)
{
     [[[msgArray objectAtindex:i] objectForKey:@"message"]  stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
}

[self.mainJSONDic setValue:msgArray forKey:@"messagesinconversation"];

尝试此代码可能对您的情况有帮助: