Iphone 关于For循环和NSMutableDictionary的帮助

Iphone 关于For循环和NSMutableDictionary的帮助,iphone,objective-c,xcode,ios4,nsmutabledictionary,Iphone,Objective C,Xcode,Ios4,Nsmutabledictionary,我正在使用for循环(当前)NSLog NSArray的内容。但是,我想将数组的内容设置到NSMutableDictionary中,具体取决于它是objectAtIndex。目前数组中有843个对象,因此我不希望重复输入相同的内容 我目前的代码是 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; string = [string stringByReplacingOccurrencesOfString:@"\n" wi

我正在使用for循环(当前)NSLog NSArray的内容。但是,我想将数组的内容设置到NSMutableDictionary中,具体取决于它是objectAtIndex。目前数组中有843个对象,因此我不希望重复输入相同的内容

我目前的代码是

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSArray *chunks = [string componentsSeparatedByString:@","];
for (int i = 0; i < [chunks count]; i ++) {
    NSLog(@"%@", [chunks objectAtIndex:i]);
}

由于您的键没有模式,因此最好像现在这样手动操作。

最简单的方法是使用您发布的代码。但是如果你真的想使用循环,像这样的东西应该可以

NSArray *keys = [NSArray arrayWithObjects:@"type", @"name", @"street", @"address1", @"address2", @"town", @"county", @"postcode", @"number", @"coffee club", @"latitude", @"longitude", nil];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSArray *chunks = [string componentsSeparatedByString:@","];
for (int i = 0; i < [chunks count] && i < [keys count]; i ++) {
    [dict setObject:[chunks objectAtIndex:i] forKey:[keys objectAtIndex:i]];
}
NSArray*键=[NSArray数组,其对象为:@“类型”@“名称”@“街道”@“地址1”@“地址2”@“城镇”@“县”@“邮政编码”@“编号”@“咖啡俱乐部”@“纬度”@“经度”,零];
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];
string=[string stringByReplacingOccurrencesOfString:@“\n”和string:@”“;
NSArray*chunks=[string componentsSeparatedByString:@“,”];
对于(int i=0;i<[chunks count]&&i<[keys count];i++){
[dict setObject:[chunks objectAtIndex:i]forKey:[keys objectAtIndex:i]];
}
NSArray*键=[NSArray数组,其对象为:@“类型”@“名称”@“街道”@“地址1”@“地址2”@“城镇”@“县”@“邮政编码”@“编号”@“咖啡俱乐部”@“纬度”@“经度”,零];
对于(int i=0;i<[块计数];i++){
[dict setObject:[chucks objectAtIndex:i]forKey:[keys objectAtIndex:i]];
}

我不确定我是否完全理解这个问题,但我认为您的
数组包含一个很长的数据列表,顺序相同(即第0、第12、第24、第36…个元素都是
类型
,第1、第13、第25、第37…个元素都是
名称
)。如果是这种情况,您可以使用以下内容:

NSArray *keys = [NSArray arrayWithObjects:@"type", @"name", @"street", @"address1", @"address2", @"town", @"county", @"postcode", @"number", @"coffee club", @"latitude", @"longitude", nil];

for (NSUInteger i = 0; i < [chunks count]; i += [keys count])
{
    NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count])];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjects:subarray forKeys:keys];
    
    // do something with dict

    [dict release];
}

你的问题是什么?在我看来,您的代码已经完全按照您的要求执行了。问题是数组中有843个不同的对象。我的意思是,没有逻辑模式。如果你在做像A、B、C、D之类的事情,就会容易得多。对于计算机来说,标签没有逻辑级数,如果这有意义的话。您可以将所有键存储在
.plist
中,并按照@Anomie的建议调用它们。我认为这会起作用,但我得到了一个错误:**-[NSArray subrayWithRange::]:索引9779超出边界[0..9776]“有没有办法修复它?@XcodeDev:我想这可能是因为你的数组中有奇数个项目。”。理想情况下,数组大小应该是12的倍数,因为有12个键。我会更新答案。谢谢你的编辑,但是现在键与它们的“块”不匹配,而且一些“块”没有进入字典。i、 在大多数情况下都是e电话号码@XcodeDev:您确定数据以正确的顺序出现在数组中,即索引0、12、24、36包含
类型的正确值,以及索引1、13、25、37包含
名称的正确值吗?@XcodeDev:数据格式不正确,该文件的第一行包含13个单独的字段,第二行是12。我认为地址中可能有逗号,导致拆分出错。
NSArray* keys = [NSArray arrayWithObjects:@"type",@"name",@"street",@"address1",@"address2",@"town",@"county",@"postcode",@"number",@"coffee club",@"latitude",@"longitude",nil];

    for (int i = 0; i < [chunks count]; i ++){
        [dict setObject:[chucks objectAtIndex:i] forKey:[keys objectAtIndex:i]]; 
    }
NSArray *keys = [NSArray arrayWithObjects:@"type", @"name", @"street", @"address1", @"address2", @"town", @"county", @"postcode", @"number", @"coffee club", @"latitude", @"longitude", nil];

for (NSUInteger i = 0; i < [chunks count]; i += [keys count])
{
    NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count])];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjects:subarray forKeys:keys];
    
    // do something with dict

    [dict release];
}
// max should be a multiple of 12 (number of elements in keys array)
NSUInteger max = [chunks count] - ([chunks count] % [keys count]);
NSUInteger i = 0;

while (i < max)
{
    NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count])];
    NSDictionary *dict = [[NSDictionary alloc] initWithObjects:subarray forKeys:keys];
    
    // do something with dict

    [dict release];
    
    i += [keys count];
}