Question

I have a NSPopUpButton that I want to use to select the text encoding for opening a file.

I already have some ideas how to implement this, but as I'm starting to learn Objective-C and Cocoa I'm almost sure that there is a better way to accomplish what i want.

I need to have a NSString with the name of the encoding and an associated NSStringEncoding value.

I have thought creating a class representing an encoding (name and value) and have a NSArray with objects of this type and then populate the NSPopUpButton with the contents of the array, but I think that there should be a better way.

I'm not very familiar with the NSDictionary class but I suspect that should make things easier.

Can someone give me a hint on this?

Was it helpful?

Solution

Create the dictionary with encodings as value and the names for the NSPopUpButton as keys

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
NSNumber numberWithLong:NSASCIIStringEncoding], @"ASCII", 
[NSNumber numberWithLong:NSUnicodeStringEncoding], @"Unicode", nil];

Then add them to the NSPopUpButton with

[myPopUpButton addItemsWithTitles:[dict allKeys]]

Then get the encoding the user selected with

[dict objectForKey:[myPopUpButton titleOfSelectedItem]]

Note: you will need to wrap the string encoding enum in a object, like NSValue or NSNumber.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top