开发者

Help with a For loop and NSMutableDictionary

开发者 https://www.devze.com 2023-02-15 00:12 出处:网络
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

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"];
[di开发者_JAVA技巧ct 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'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];
}


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.

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

精彩评论

暂无评论...
验证码 换一张
取 消