BeachRunnerFred
BeachRunnerFred

Reputation: 18578

Clean way to construct NSDictionary from array containing interleaved keys and values

I have an array of keys/values...

NSArray *keysAndValues = @[@"1", @"one", @"2", @"two", @"3", @"three"];

What's the simplest way to convert this to an NSDictionary so that it matches a dictionary that looks like...

NSDictionary *dict = @{@"1" : @"two", @"2" : @"two", @"3" : @"three"};

I have code that just cycles through the array and extracts the key/values, but I was wondering if there's a cleaner way or any built in NSDictionary methods that I'm missing?

Upvotes: 1

Views: 2424

Answers (3)

jscs
jscs

Reputation: 64002

You're not missing anything. There isn't any really convenient way other than looping to do this. Of course any solution would be, at root, looping, but it would be nice if there were some more "filtering" or re-arranging abilities built in to NSArray (/me coughs discreetly and looks at Python list comprehensions).

There's likely to be something useful in this vein in vikingosegundo's arraytools on GitHub, or similar collection-manipulation extensions by others. There's certainly no harm in writing your own category method to do this, though, like I said, there would have to be a loop somewhere in there.

Here's my suggestion, for whatever that's worth. Split the array by enumerating it:

NSMutableArray * keys = [NSMutableArray array];
NSMutableArray * values = [NSMutableArray array];
[array enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop)){
    if( idx % 2 ){
        [values addObject:obj];
    else {
        [keys addObject:obj];
    }
}];

NSDictionary * d = [NSDictionary dictionaryWithObjects:values forKeys:keys];

Seems like there should be a better way to do this, such as with an NSIndexSet and objectsAtIndexes:, but there's no convenient way to create an index set with a non-contiguous bunch of indexes.

Something like this, e.g., would be nice:

@implementation NSIndexSet (WSSNonContiguous)


+ (id) WSS_indexSetWithOddIndexesInRange: (NSRange)range
{
    NSMutableIndexSet * s = [NSMutableIndexSet indexSet];

    // If the start of the range is even, start with the next number.
    NSUInteger start = range.location % 2 ? range.location : range.location + 1;
    NSUInteger upper_limit = range.location + range.length;
    for( NSUInteger i = start; i < upper_limit; i += 2 ){
        [s addIndex:i];
    }

    return s;
}

+ (id) WSS_indexSetWithEvenIndexesInRange: (NSRange)range
{
    NSMutableIndexSet * s = [NSMutableIndexSet indexSet];

    // If the start of the range is odd, start with the next number.
    NSUInteger start = range.location % 2 ? range.location + 1 : range.location;
    NSUInteger upper_limit = range.location + range.length;
    for( NSUInteger i = start; i < upper_limit; i += 2 ){
        [s addIndex:i];
    }

    return s;
}

@end

With a helper function:

NSRange rangeOfNumbers(NSUInteger start, NSUInteger end)
{
    return (NSRange){start, end-start};
}

So that you can do this:

NSIndexSet * s = [NSIndexSet WSS_indexSetWithEvenIndexesInRange:rangeOfNumbers(0, 10)];
NSLog(@"%@", s);
// Prints: <NSMutableIndexSet: 0x7fccca4142a0>[number of indexes: 5 (in 5 ranges), indexes: (0 2 4 6 8)]

Upvotes: 3

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

There is no built-in method in NSDictionary that takes alternate values from an array and break it into 2 sets, one for key and another for value.

You have to do something like this: (This is not compiler tested, but will give you an idea.)

If you want key = 1 and object = one :

for(int i=0;i<keysAndValues.count;i+=2){
    [dict setObject:[keysAndValues objectAtIndex:i+1] forKey:[keysAndValues objectAtIndex:i]];
}

Upvotes: 3

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21976

I have code that just cycles through the array and extracts the key/values, but I was wondering if there's a cleaner way or any built in NSDictionary methods that I'm missing?

No way out, you have to iterate through all the array.

If you are searching for a cleaner way probably Josh Caswell's answer is the best one, but you still have to iterate over the array.

That would be easy to do using this method:

+ (id)dictionaryWithObjectsAndKeys:(id)firstObject , ... 

But unfortunately there isn't a straight way to create a va_list from an array. See also this question.

Upvotes: 1

Related Questions