【发布时间】: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