3

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?

Bruno Ferreira
  • 942
  • 9
  • 22

1 Answers1

6

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.

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Frank
  • 2,375
  • 1
  • 19
  • 17
  • NSStringEncoding values are not objects, so I guess I will have to use `[NSNumber numberWithLong:NSASCIIStringEncoding]` for the object of the dictionaryWithObjectsAndKeys methode. Right? – Bruno Ferreira Jul 13 '12 at 18:30
  • Oops, yeah you need to wrap them in a NSNumber or NSValue. – Frank Jul 16 '12 at 07:05