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?
The following worked for me:
bool someValue;
NSObject objValue = NSObject.FromObject(someValue);
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With