1

Is it possible to replicate what the code in here does in MonoTouch?

Here is what I've tried so far:

foreach(string countryCode in NSLocale.ISOCountryCodes){
 // How to convert this objective-c code to c#?
 // [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:countryCode]
}
Community
  • 1
  • 1
John Simons
  • 4,288
  • 23
  • 41
  • It seems possible, there's an NSLocale class etc... what have you tried? Are you hitting a specific error? – bryanmac Sep 28 '11 at 02:22
  • Question updated to include c# code so far – John Simons Sep 28 '11 at 03:03
  • yeah - I started to type the answer (knowing C# but not Mono expert) and I got as far as you. Docs look missing and couldn't find how to translate the country code into a friendly description string. As you iterate, add the description (if you figure that out) to a List and sort that. – bryanmac Sep 28 '11 at 03:08

2 Answers2

2

Sadly a quick look shows that displayNameForKey:value: to be (currently as of 4.2.x) missing from MonoTouch (and MonoMac) bindings. I'll look into implementing it and will update this entry once done.

UPDATE : Source code to work around the missing binding

    public void DisplayCountryCodeNames ()
    {
        NSLocale current = NSLocale.CurrentLocale;
        IntPtr handle = current.Handle;
        IntPtr selDisplayNameForKeyValue = new Selector ("displayNameForKey:value:").Handle;
        foreach (var countryCode in NSLocale.ISOCountryCodes) {
            using (var key = new NSString ("kCFLocaleCountryCodeKey")) {
                using (var nsvalue = new NSString (countryCode)) {
                    string ret = NSString.FromHandle (MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr (handle, selDisplayNameForKeyValue, key.Handle, nsvalue.Handle));
                    Console.WriteLine ("{0} -> {1}", countryCode, ret);
                }
            }
        }
    }

Adapt to your liking and have fun with MonoTouch!

p.s. I'll update the bindings so it will be included in future releases for MonoTouch in a more proper API ;-)

poupou
  • 43,413
  • 6
  • 77
  • 174
  • Btw, looking at implementing a Countries picklist, has anyone done this before? – John Simons Sep 29 '11 at 00:08
  • That should be made another question (so more people will read it than this comment) – poupou Sep 29 '11 at 20:28
  • Was this binding ever added to MonoTouch? I currently need to get the names of currencies (NSLocaleCurrencyCode), but the above code, even with "kCFLocaleCurrencyCodeKey", isn't working for me. – Chris Wenham Aug 18 '12 at 18:44
0

Yes, it is certainly possible. MonoTouch wraps the iOS API in C# classes so that you can do anything in C# that you can do in Objective-C. Unfortunately you have to download the trial version to get the documentation. I imagine the code would look something like this:

foreach(string countryCode in NSLocale.ISOCountryCodes){
  string CountryName = NSLocale.displayNameForKey(NSLocaleCountryCode, countryCode);
}
J Edward Ellis
  • 1,368
  • 2
  • 12
  • 21