objective-c更改循环内部的数组内容
我有一个问题(我认为)可能与范围有关,但我不确定。我试图做一些我认为应该简单的事情,但我得到了一个奇怪的结果,我真的需要一些建议。我会说我是一个早期的objective-c程序员,但不是一个完全的新手 我已经用objective-c编写了一个函数,我想用它来更改可变字典对象的可变数组中的键名。因此,我想传入一个可变字典对象的可变数组,并返回具有相同字典对象的相同可变数组,但某些键名已更改。有道理吗 我在这段代码中尝试了几个log语句,它们似乎表明我所做的一切都在工作,除了for循环执行完毕(当我尝试测试temp数组中的值时),数组似乎只包含源数组中的最后一个元素,重复了[source count]次。通常,这会让我相信我没有正确地编写新值,或者没有正确地读取它们,或者甚至我的NSLog语句没有显示我认为它们是什么。但这可能是因为范围吗?数组是否在for循环之外保留其更改 我已经在这个函数中花费了相当多的时间,我已经用尽了我的技巧。有人能帮忙吗objective-c更改循环内部的数组内容,objective-c,loops,for-loop,scope,nsmutablearray,Objective C,Loops,For Loop,Scope,Nsmutablearray,我有一个问题(我认为)可能与范围有关,但我不确定。我试图做一些我认为应该简单的事情,但我得到了一个奇怪的结果,我真的需要一些建议。我会说我是一个早期的objective-c程序员,但不是一个完全的新手 我已经用objective-c编写了一个函数,我想用它来更改可变字典对象的可变数组中的键名。因此,我想传入一个可变字典对象的可变数组,并返回具有相同字典对象的相同可变数组,但某些键名已更改。有道理吗 我在这段代码中尝试了几个log语句,它们似乎表明我所做的一切都在工作,除了for循环执行完毕(当我
-(NSMutableArray *)renameKeysIn:(NSMutableArray*)source {
/*
// Pre:
// The source array is an array of dictionary items.
// This method renames some of the keys in the dictionary elements, to make sorting easier later.
// - "source" is input, method returns a mutable array
*/
// copy of the source array
NSMutableArray *temp = [source mutableCopy];
// a temporary dictionary object:
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
// These arrays are the old field names and the new names
NSMutableArray *originalField = [NSMutableArray arrayWithObjects:@"text", @"created_at",nil];
NSMutableArray *replacedField = [NSMutableArray arrayWithObjects:@"title", @"pubDate", nil];
// loop through the whole array
for (int x =0; x<[temp count]; x++) {
// set the temp dictionary to current element
[dict setDictionary:[temp objectAtIndex:x]];
// loop through the number of keys (fields) we want to replace (created_at, text)... defined in the "originalField" array
for (int i=0; i<[originalField count]; i++)
{
// look through the NSDictionary item (fields in the key list)
// if a key name in the dictionary matches one of the ones to be replaced, then replace it with the new one
if ([dict objectForKey:[originalField objectAtIndex:i]] != nil) {
// add a new key/val pair: the new key *name*, and the old key *value*
[dict setObject:[dict objectForKey:[originalField objectAtIndex:i]]
forKey:[replacedField objectAtIndex:i]];
// remove the old key/value pair
[dict removeObjectForKey:[originalField objectAtIndex:i]];
}// end if dictionary item not null
}// end loop through keys (created_at, text)
[temp replaceObjectAtIndex:x withObject:dict];
}// end loop through array
// check array contents
for (int a=0; a<[temp count]; a++){
NSLog(@"Temp contents: ############ %@",[[temp objectAtIndex:a] objectForKey:@"pubDate"]);
}
return temp;
} // end METHOD
-(NSMutableArray*)重命名Keysin:(NSMutableArray*)源{
/*
//前:
//源数组是字典项的数组。
//此方法重命名字典元素中的一些键,以便以后更容易排序。
//-“source”是输入,方法返回可变数组
*/
//源数组的副本
NSMutableArray*temp=[源mutableCopy];
//临时字典对象:
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];
//这些数组是旧字段名和新字段名
NSMutableArray*originalField=[NSMutableArray arrayWithObjects:@“text”,@“created_at”,nil];
NSMUTABLEARRY*replacedField=[NSMUTABLEARRY数组及其对象:@“title”,@“pubDate”,nil];
//在整个阵列中循环
对于(int x=0;x我认为问题在于:
[dict setDictionary:[temp objectAtIndex:x]];
因为这些东西几乎都在指针中工作(而不是复制内容),所以临时数组的每个元素都将指向dict字典,dict字典被设置为最新键的字典。我认为设置实际指针将解决这个问题
dict = [temp objectAtIndex:x];
是的-就是这样,非常感谢。我不得不做一个小改动-我不得不添加“mutableCopy”,因为它在我试图修改dict时就崩溃了。再次感谢!