有关 For 循环和 NSMutableDictionary 的帮助
我正在使用 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"];
I am using a for loop to (currently) NSLog the contents of a NSArray. However I would like to set the contents of the array into a NSMutableDictionary, depending on the objectAtIndex it is. Currently there are 843 objects in the array, and therefore I would rather not have to type out the same thing over and over again!
My code currently is this
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]);
}
I would like to set the contents of the array into the NSMutableDictionary in the following fashion, and once the objectAtIndex is 11, I would like to set the 12th object in the dictionary to be of the key @"type" and soforth:
[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"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定我完全理解这个问题,但我认为您的 chunks 数组包含一长串数据,以相同的方式排序(即第 0 个、第 12 个、第 24 个、第 36 个...元素都是
type
,第1、13、25、37...元素都是name
)。如果是这种情况,您可以使用如下内容:请注意,对于
NSDictionary
,同一个键不能有两个不同的值。也就是说,如果您为type
键设置两个不同的值,则仅保留最后设置的值。编辑
如果您的数组不是 12 的倍数,因为例如它在末尾包含垃圾数据,您可以使用不同的循环样式:
I'm not sure I fully understand the question, but I think that your
chunks
array contains a long list of data, ordered in the same way (i.e. 0th, 12th, 24th, 36th... elements are alltype
, and 1st, 13th, 25th, 37th... elements are allname
). If this is the case, you could use something like this:Note that you can't have two different values for the same key with
NSDictionary
. That is, if you set two different values for thetype
key, only the last value set will be kept.Edit
If your array is not a multiple of 12 because for example it contains garbage data at the end, you could use a different looping style instead:
由于您的按键没有模式,因此您最好像现在一样手动执行此操作。
Since there's no pattern to your keys, you're better off doing it manually like you're doing it now.
最简单的方法是使用您发布的代码。但如果你真的想使用循环,应该这样做。
The most straightforward thing to do would be to use the code you posted. But if you really want to use a loop, something like this should do.