有关 For 循环和 NSMutableDictionary 的帮助

发布于 2024-10-20 17:44:57 字数 1338 浏览 3 评论 0原文

我正在使用 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幽梦紫曦~ 2024-10-27 17:44:57

我不确定我完全理解这个问题,但我认为您的 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];
}

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 all type, and 1st, 13th, 25th, 37th... elements are all name). If this is the case, you could use something like this:

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

Note that you can't have two different values for the same key with NSDictionary. That is, if you set two different values for the type 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:

// 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];
}
忘东忘西忘不掉你 2024-10-27 17:44:57

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

Since there's no pattern to your keys, you're better off doing it manually like you're doing it now.

转角预定愛 2024-10-27 17:44:57

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

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

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.

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]];
}
凯凯我们等你回来 2024-10-27 17:44:57
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 (int i = 0; i < [chunks count]; i ++){
        [dict setObject:[chucks objectAtIndex:i] forKey:[keys objectAtIndex:i]]; 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文