nsuserdefaults

How to deal with non-optional values in NSUserDefaults in Swift

时间秒杀一切 提交于 2020-01-03 03:08:05
问题 To get a value from NSUserDefaults I would do something like this: let userDefaults = NSUserDefaults.standardUserDefaults() if let value = userDefaults.objectForKey(key) { print(value) } However, these methods do not return optionals: boolForKey (defaults to false if not set) integerForKey (defaults to 0 if not set) floatForKey (defaults to 0.0 if not set) doubleForKey (defaults to 0.0 if not set) In my app I want to used the saved value of an integer if it has been previously set. And if it

cloudkit: how to access main user's attributes?

大城市里の小女人 提交于 2020-01-03 00:50:54
问题 I am trying to make an app in which every user would have a personal avatar, a nickname and so on. I don't know how to access the user's account on cloudkit. My solution would be to retrieve user's AppleID, query Cloudkit and modify the user's attribute... But I guess there should be a better way to do it. Isn't their something like NSUserDefault but for cloudkit? 回答1: You first have to find out what the current loged in userId is. You can do that by executing the

Store and update a Swift Dictionary in NSUserDefaults Xcode

大城市里の小女人 提交于 2020-01-02 08:40:08
问题 I would like to store and update a Dictionary when a user enters a value. Everything seems to function until this code, and the application crashes: override func viewDidLoad() { super.viewDidLoad() if NSUserDefaults.standardUserDefaults().objectForKey("dict") != nil { answersSaved = NSUserDefaults.standardUserDefaults().objectForKey("dict") as [String:String] } } The error message says "anyObject is not convertible to [String:String]. It offers to add ! after the as but then the app crashes.

Swift 4 Settings Bundle, get defaults

冷暖自知 提交于 2020-01-02 03:30:33
问题 I created a settings bundle with about 8 toggle switches. What I am trying to do it get the default values from the settings bundle. Currently right now I have these two methods: func registerSettingsBundle(){ let appDefaults = [String:AnyObject]() UserDefaults.standard.register(defaults: appDefaults) UserDefaults.standard.synchronize() } func updateDisplayFromDefaults(){ let defaults = UserDefaults.standard let update_lot = defaults.bool(forKey: "update_lot") print(update_lot) } and I am

Understanding the UserDefaults register method

走远了吗. 提交于 2020-01-02 03:12:10
问题 First of all, let me say that this is NOT a complaint, I just want to understand how does the register(defaults: []) method works. I'm using the UserDefaults register(defaults: []) method inside the didFinishLaunchingWithOptions method to register the default value of all of my UseDefault keys, everything is working fine as expected. My question is, why is that the values in the register(defaults: []) method do not reset everytime the didFinishLaunchingWithOptions method is called? I do not

Understanding the UserDefaults register method

大憨熊 提交于 2020-01-02 03:12:07
问题 First of all, let me say that this is NOT a complaint, I just want to understand how does the register(defaults: []) method works. I'm using the UserDefaults register(defaults: []) method inside the didFinishLaunchingWithOptions method to register the default value of all of my UseDefault keys, everything is working fine as expected. My question is, why is that the values in the register(defaults: []) method do not reset everytime the didFinishLaunchingWithOptions method is called? I do not

save bool in nsuserdefaults

左心房为你撑大大i 提交于 2020-01-02 03:01:04
问题 when my app starts music is playing: -(void)playBgMusic { NSString *path = [[NSBundle mainBundle] pathForResource:@"bgmusic" ofType:@"aif"]; theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; theAudio.delegate = self; [theAudio play]; } but he should be able to turn the music off by pressing a button if he presses the button again the music should turn on again. i have: -(IBAction)check { if (isquiet == NO) { [theAudio stop]; isquiet = YES;

How to get an update NSUserDefault on iOS 4?

风流意气都作罢 提交于 2020-01-01 04:47:04
问题 I am developing an iPhone application, I encounter a problem on the iOS4 because of multi task. This application has the default settings defined in a Settings.bundle. If I run my application then I left it (so it goes in the background). I'll change the settings and restarts the application (it comes out of standby and method: applicationDidBecomeActive () is called). Values in NSUserDefault are not updates, but when I leave the application and relaunch. The values are good. Does someone

load/save settings in iOS

喜夏-厌秋 提交于 2019-12-31 08:08:11
问题 I have the following two procedures defined in my AppDelegate. saveSettings and loadSettings. I am calling my loadSettings procedure in the AppDidFinishLaunching method, and I am calling the saveSettings procedure in my settings view, once the save button is clicked. Both methods seem to be called at the right time, the right number of times (once), and using the correct data. my settings object gets the right data, but the data does not seem to be actually saving. When I run the load code,

NSUserDefaults Settings Bundle Plist

佐手、 提交于 2019-12-31 03:52:06
问题 I have an app that checks for a file upon load from the appDelegate's appDidFinishLoading method using a url value that I supposedly store in the NSUserDefaults Settings Root Plist: NSString *pathStr = [[NSBundle mainBundle] bundlePath]; NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:@"Settings.bundle"]; NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"Root.plist"]; NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile