1

I have an Array, which consists of object,key,object,key,object,key.

How to init NSMutableDictionary with this array?

I found methods for separated arrays of keys(ArrayOfKeys) and objects(ArrayOfObjects), but not for this mixed Array.

also ObjectsAndKeys maybe help, but i don't know how.

Thanks for help!

2 Answers 2

4
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for(int i=0; i < [array length] - 1; i += 2)
{
    [dictionary setObject:[array objectAtIndex:i+1] forKey:[array objectAtIndex: i]];
}
Sign up to request clarification or add additional context in comments.

1 Comment

2

Just create two arrays: keysArray (odd indexes) and valuesArray (even indexes) and use [NSMutableDictionary dictionaryWithObjects:valuesArray forKeys:keysArray]

You can do this with a single for loop...

10 Comments

i know, but i want to avoid tricks like this. I think, that Apple libraries are more flexible, oh
@loldop In this particular case there is no initializer that does what you want. You'll need to do it yourself.
Flexibility doesn't mean giving anything you want
@Jun1st it is strange, that i have method ObjectsAndKeys and doesn't have method ObjectsAndKeysFromOneArray. i want this method, cause it simple to realization and simple to read.
@loldop, it's definitely simple. but it takes time to do a thing, no matter how simple it is. Frameworks are built by developers, and developers are lazy:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.