【问题标题】:Help with a For loop and NSMutableDictionary帮助 For 循环和 NSMutableDictionary
【发布时间】:2011-03-07 01:57:11
【问题描述】:

我正在使用 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]);
}

我想按以下方式将数组的内容设置到 NSMutableDictionary 中,一旦 objectAtIndex 为 11,我想将字典中的第 12 个对象设置为键 @"type" 等等:

[dict setObject:[chunks objectAtIndex:0] forKey:@"type"];
[dict setObject:[chunks objectAtIndex:1] forKey:@"name"];
[dict setObject:[chunks objectAtIndex:2] forKey:@"street"];
[dict setObject:[chunks objectAtIndex:3] forKey:@"address1"];
[dict setObject:[chunks objectAtIndex:4] forKey:@"address2"];
[dict setObject:[chunks objectAtIndex:5] forKey:@"town"];
[dict setObject:[chunks objectAtIndex:6] forKey:@"county"];
[dict setObject:[chunks objectAtIndex:7] forKey:@"postcode"];
[dict setObject:[chunks objectAtIndex:8] forKey:@"number"];
[dict setObject:[chunks objectAtIndex:9] forKey:@"coffee club"];
[dict setObject:[chunks objectAtIndex:10] forKey:@"latitude"];
[dict setObject:[chunks objectAtIndex:11] forKey:@"longitude"];

【问题讨论】:

  • 您的问题是什么?在我看来,您那里的代码已经完全符合您的要求。

标签: iphone objective-c xcode ios4 nsmutabledictionary


【解决方案1】:

我不确定我是否完全理解这个问题,但我认为您的 chunks 数组包含一长串数据,以相同的方式排序(即第 0、第 12、第 24、第 36 ......元素都是type,以及第 1、13、25、37... 元素都是name)。如果是这种情况,您可以使用以下内容:

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];
}

请注意,NSDictionary 的同一个键不能有两个不同的值。也就是说,如果您为 type 键设置了两个不同的值,则只会保留最后一个设置的值。

编辑

如果您的数组不是 12 的倍数,因为例如它在末尾包含垃圾数据,您可以改用不同的循环样式:

// 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];
}

【讨论】:

  • 我认为它会起作用,但我收到错误:*** -[NSArray subarrayWithRange:]: index 9779 beyond bounds [0 .. 9776]' 知道如何解决它吗?
  • @XcodeDev:我认为这可能是因为您的数组中有奇数个项目。理想情况下,数组大小应该是 12 的倍数,因为有 12 个键。我会更新答案。
  • 感谢您的编辑,但现在键与它们的“块”不匹配,并且一些“块”没有进入字典。即大多数情况下的电话号码!
  • @XcodeDev:你确定数据在数组中出现的顺序是正确的,即索引0、12、24、36包含type的正确值,和1, 13、25、37 包含 name 的正确值?
  • @XcodeDev:数据格式不正确,该文件第一行包含13个单独的字段,第二行包含12个。我想可能是地址中有逗号,导致分裂出问题。
【解决方案2】:

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

【讨论】:

  • 问题是数组中有843个不同的对象。
  • 嗯,我的意思是,没有逻辑模式。如果您正在执行 A、B、C、D 等类似的操作,那就容易多了。对于计算机而言,您的标签没有逻辑上的进展,如果这有任何意义的话。您可以做的是将所有密钥存储在 .plist 中,然后按照 @Anomie 的建议来回忆它们。
【解决方案3】:

最直接的做法是使用您发布的代码。但如果你真的想使用循环,应该这样做。

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]];
}

【讨论】:

    【解决方案4】:
    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]]; 
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 2011-04-20
      • 2015-03-30
      • 2018-07-17
      • 2021-05-31
      • 1970-01-01
      相关资源
      最近更新 更多