开发者

objective-c how to create an enumeration array with its string values as keys?

开发者 https://www.devze.com 2023-01-27 10:14 出处:网络
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.

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] ;
NS开发者_开发知识库Array* 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.


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


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!).


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]
0

精彩评论

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