Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert bool to NSObject in MonoTouch

This is making me a little crazy. I'm trying to register some NSUserDefaults in my MonoTouch app.

var defaultsToRegister = new NSMutableDictionary();
defaultsToRegister.SetValueForKey(true, IS_REMINDING);
NSUserDefaults.StandardUserDefaults.RegisterDefaults(defaultsToRegister);

Obviously this isn't compiling because a bool cannot be converted to NSObject. But when I want to set or retrieve a boolean value from NSUserDefaults I can use:

public bool IsReminding {
    get {
        return NSUserDefaults.StandardUserDefaults.BoolForKey(IS_REMINDING);
    }

    set {
        NSUserDefaults.StandardUserDefaults.SetBool(value, IS_REMINDING);
    }
}

In the settings plist file the boolean is written as <key>ReminderTimeOfDay</key><true/> which is what I would expect. So how to register a default boolean value?

like image 873
David Clarke Avatar asked Nov 12 '12 22:11

David Clarke


2 Answers

The following worked for me:

 bool someValue;
 NSObject objValue = NSObject.FromObject(someValue);
like image 133
lost_bits1110 Avatar answered Oct 28 '22 16:10

lost_bits1110


That's how I do it:

using (var defaults = NSDictionary.FromObjectsAndKeys (new object[] {
        true,
    }, new object [] {
        "AllowInternetAccess",
    })) {

    settings.RegisterDefaults (defaults);
}

Of course I have more than one key/value pair and this allows me to avoid several calls to a NSMutableDictionary.

like image 34
poupou Avatar answered Oct 28 '22 15:10

poupou