0

i have an enumeration say gender, now i want to associate it to string values to use in the view inside a picker view. It's cocoa-touch framework and objective-c as language. So i don't know of a way to set the data source of the picker view as the enumeration, as could have been done in other frameworks. So i've been told i have to make array of enum values. and then i tried to add thos into an NSMutableDictionary with their respective string values. So i ended up with

NSArray* genderKeys = [NSArray arrayWithObjects:@"Male",@"Female",nil] ;
NSArray* genderValues = [NSArray arrayWithObjects:[NSNumber numberWithInt:male],[NSNumber numberWithInt:female],nil];

for(int i =0;i<[genderKeys count];i++)
    [_genderDictionary setValue:[genderValues objectAtIndex:i] forKey:[genderKeys objectAtIndex:i]];

and it's not working saying it's not a valid key, and i've read the key-coding article and i know now what's key and whats keypath, but still how can i solve that. It's ruining my life, Please help.

Sorry guys, i was using NSDictionary for _genderDictionary.But i had in my mind that it was nsmutable. Thank you all.

3 Answers 3

2

Be careful using UI text as keys into your database. What amount when you need to localise your application to french, chinese, arabic etc?

Sign up to request clarification or add additional context in comments.

1 Comment

dictionary keys shouldn't really ever be something that is exposed to the user. Not because of the language issue so much as because it's bass-ackwards. The key is what you use to retrieve the data, not the data itself.
0

That works for me. Running this (your code, with the first line added so it would compile) seems to work fine.

NSMutableDictionary *_genderDictionary = [NSMutableDictionary dictionary];
NSArray* genderKeys = [NSArray arrayWithObjects:@"Male",@"Female",nil] ;
NSArray* genderValues = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil];

for(int i =0;i<[genderKeys count];i++)
    [_genderDictionary setValue:[genderValues objectAtIndex:i] forKey:[genderKeys objectAtIndex:i]];

NSLog()-ing _genderDictionary outputs this

{
    Female = 2;
    Male = 1;
}

edit: re-reading your question, makes me think what you are looking for is the delegate methods of UIPickerView... implementing –pickerView:titleForRow:forComponent: is where you set the text that appears in the picker. If you have an NSArray of genders, you would do something like return [_genderArray objectAtIndex:row]; That way you don't need to fuss around with a dictionary and keys.

edit 2: a picker's datasource can't be an NSArray or NSDictionary directly. It has to be an object that implements UIPickerView's datasource/delegate protocol (which I suppose you could do with a subclass of NSArray, but that'd be cah-ray-zay!).

Comments

0

If I understand you correctly, you try to create a pre-populated dictionary.
You could use [NSDictionary dictionaryWithObjectsAndKeys:] for that.

[NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithUnsignedInt:0], @"Male", 
    [NSNumberWithUnsignedInt:1], @"Female", nil]

Comments

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.