nsuserdefaults

Does NSUserDefaults save objects if the application becomes inactive?

江枫思渺然 提交于 2019-12-23 02:09:31
问题 For example: NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; [user setObject:textField1.text forKey:@"receive_text1"]; [user setObject:textField2.text forKey:@"receive_text2"]; [user synchronize]; If I leave this app, just like from background to foreground, or relaunching the app Could objects in NSUserDefaults still get the data that I set before? 回答1: NSUserDefault is persistent. So the data will be saved until you remove them or delete the app from the device. 来源: https:/

Clicking on Like all the view should show like objective C

こ雲淡風輕ζ 提交于 2019-12-23 01:11:52
问题 When I select Throwback -> Description occurs if I click on like button the like button changes to unlike and " like count=1099" increases. If I press back I want this unlike to be displayed next to Throwback let say in a label and again select Throwback button should display unlike and like Count should be 1100. please Help how can i achieve this ? //DetailOfUser.m #impot"DetailsOfStories.h" @interface DetailOfUser ()<UITableViewDelegate,UITableViewDataSource> { NSMutableArray

Cannot retrieve data from NSUserDefaults into today extension (Swift)

孤街浪徒 提交于 2019-12-22 10:28:15
问题 I have a group of arrays that all contain a number of instances of a custom Task object that I have created. I am saving the arrays to NSUserDefaults as follows: Custom Task Object class Task:NSObject, NSCoding { var name:String var notes:String var date:NSDate var dateUse:Bool var taskCompleted:Bool init(name:String, notes:String, date:NSDate, dateUse:Bool, taskCompleted:Bool){ self.name = name self.notes = notes self.date = date self.dateUse = dateUse self.taskCompleted = taskCompleted }

Call function when if value in NSUserDefaults.standardUserDefaults() changes

不羁的心 提交于 2019-12-22 10:03:29
问题 I have a variable that become value from NSUserDefaults.standardUserDefaults() var GiftCount = NSUserDefaults.standardUserDefaults().valueForKey("Gift") as! Int And i have a function named setGiftCount()... I need call this function when variable GiftCount has changed... How to do it? 回答1: First NSUserDefaults.standardUserDefaults().addObserver(self, forKeyPath: "Gift", options: NSKeyValueObservingOptions.New, context: nil) Second deinit { NSUserDefaults.standardUserDefaults().removeObserver

UserDefaults/KeyedArchiver Frustrations

佐手、 提交于 2019-12-22 09:30:00
问题 I'm working on a homework app that uses custom Assignment objects for each assignment. I am trying to store an NSMutableArray (casted to an NSArray via initWithArray:) in standardUserDefaults but I'm having trouble with saving and reloading the array. I have a table view from which you can choose to add a new assignment (which loads NewAssignmentViewController). When you save the assignment, it is pushed back to an array in AssigmentsViewController. And then you call it every time you load

NSUserDefaults objectForKey sometimes nil

孤人 提交于 2019-12-22 05:53:52
问题 I've just noticed that a key that exists in my **NSUserDefaults** is returning nil quite often. It seems about half the time it's correct and the other half it's not. I say half the time, but I dont mean that its flip-flopping, its just 40-50% I see it not work. When I write the initial value, I call synchronize immediately. I use this key as the applications revision I set when a new user signs up. The following code returns nil: #define kDBrevision @"revision" NSString *rev = [

NSUserDefaults in iOS Playground

房东的猫 提交于 2019-12-22 04:27:10
问题 There seems to be a strange issue with iOS Playgrounds where NSUserDefaults always returns nil instead of the actual value. In an iOS Playground the last line wrongly returns nil . import UIKit let defaults = NSUserDefaults.standardUserDefaults() defaults.setObject("This is a test", forKey: "name") let readString = defaults.objectForKey("name") In an OSX Playground the last line correctly returns "This is a test". import Cocoa let defaults = NSUserDefaults.standardUserDefaults() defaults

Increment integer in NSUserDefaults

谁都会走 提交于 2019-12-21 18:57:50
问题 Is there a more elegant way to increment a counter stored in user defaults? let defaults = NSUserDefaults.standardUserDefaults() defaults.setInteger(defaults.integerForKey("counter")+1, forKey: "counter") 回答1: No but if you do this a lot it might make a nice extension, something like this. extension NSUserDefaults { class func incrementIntegerForKey(key:String) { let defaults = standardUserDefaults() let int = defaults.integerForKey(key) defaults.setInteger(int+1, forKey:key) } } Usage like

Change objects in NSUserDefaults without creating and re-setting copies

故事扮演 提交于 2019-12-21 13:43:14
问题 I've got dictionary stored in NSUserDefaults and I need to add/remove items in this dictionary. It bothers me that to do this I have to create mutable copy of entire dictionary, change one element and replace entire dictionary with new copy. copy = [[defaults objectForKey:@"foo"] mutableCopy]; [copy setObject:… forKey:@"bar"]; [defaults setObject:copy forKey:@"foo"]; and it involves even more copying and re-setting for objects deeper in the hierarchy. Is there a better way? I've tried to use

When the app is deleted what happens to the values stored in NSUserDefaults?

Deadly 提交于 2019-12-21 11:15:59
问题 I have a little question regarding NSUserDefaults . When the application is deleted, what happens to the values stored in the NSUserDefaults ? Do they get deleted as well? When I remove a app and reinstall it from the app store the previous NSUserDefaults values are loaded? 回答1: When an app is deleted from the device all local data associated with the app will be deleted too. This includes NSUserDefaults . However any data stored at an online service won't. For example, iCloud. The app will