nsuserdefaults

How to save in NSUserDefaults a array of object?

倾然丶 夕夏残阳落幕 提交于 2019-12-06 06:58:35
问题 I need save in NSUserDefaults an array or other structure of data to save objects with this format: name = "myName", lastName = "myLastName" I was trying to do this with an array of arrays but this can't save in NSUserDefaults . Now I am trying with a custom class with structure to create objects and append to the array, but also get an error when trying to save this to NSUserDefaults . I need save with this structure, but I don't know what is the correct form. How would I do this? var

Using NSUserDefaults to store many values will cause problems?

坚强是说给别人听的谎言 提交于 2019-12-06 06:50:14
问题 I have created an application which will login using username, password and allows the user to download the files from serve. In my application I have to persist few datas once the user have login in the application. Persisting data like Username, Password, user downloaded list , subscribed plans,etc. The data can be persisted in application either by NSUserDefaults,Plist , etc. But I feel easy to store and retrieve the values using NSUserDefaults. As I need to use the values in many view

How to store an retrieve float from NSUserDefaults

喜欢而已 提交于 2019-12-06 06:17:34
问题 I'm having trouble storing and retrieving a float in NSUserDefauts. I store the value, but when I retrieve it, it returns 0. here's what I tried and didn't work: [pref setFloat:3.0f forKey:@"key"]; float value = [pref floatForKey:@"key"]; //value=0 [pref setFloat:3 forKey:@"key"]; float value = [pref floatForKey:@"key"];//value=0 [pref setObject:[NSNumber numberWithFloat:3] forKey:@"key"]; float value = [[pref objectForKey:@"key"]floatValue];//value=0 [pref setObject:[NSNumber numberWithFloat

Swift NSUserDefaults NSArray using objectForKey

会有一股神秘感。 提交于 2019-12-06 06:15:17
I'm pretty new to Swift, and I've managed to get pretty stuck. I'm trying to retrieve data from NSUserDefaults and store it in an array ( tasks ): @lazy var tasks: NSArray = { let def = NSUserDefaults.standardUserDefaults() let obj: AnyObject? = def.objectForKey("tasks") return obj as NSArray }() All I'm getting is a warning: EXE_BAD_INSTRUCTION on line 3. Also to note that I haven't actually set any data yet, but what I'm aiming for is that if there is no data, I want the array to be empty. I'll be using the data to populate a table view. Now using a var instead of a constant: @lazy var tasks

How to maintain cell selection in NSUserDefaults?

爱⌒轻易说出口 提交于 2019-12-06 05:57:30
https://github.com/lminhtm/LMDropdownView How can I save the last selected cell in NSUserDefault? When the application is reopened the selection should be maintained. Currently, the default cell is selected whenever the app opens. - (void)viewDidLoad { [super viewDidLoad]; self.mapTypes = @[@"Standard", @"Satellite", @"Hybrid"]; self.currentMapTypeIndex = 0; self.dropPinButton.layer.cornerRadius = 5; self.removeAllPinsButton.layer.cornerRadius = 5; self.moreButton.layer.cornerRadius = 5; self.moreButton.layer.shadowOffset = CGSizeZero; self.moreButton.layer.shadowOpacity = 0.5; self.moreButton

Store and update a Swift Dictionary in NSUserDefaults Xcode

南楼画角 提交于 2019-12-06 04:24:46
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. dict is my Dictionary variable with Strings as values. I also have code updating the NSUserDefaults

OSX NSUserDefaults not Working

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 04:18:30
问题 This code gives me always NO in my application. It does indeed work in any other project I copy it... so something must be messed up with my standardUserDefaults, but I absolutely don't know how this can happen and how to solve it! NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setBool:YES forKey:@"myKey"]; BOOL test = [defaults boolForKey:@"myKey"]; // test is ALWAYS NO here! Can anybody hint me, where to start or how to get rid of this? It's a mixed project with

Is there any way to clear all data stored from the installation of app?

こ雲淡風輕ζ 提交于 2019-12-06 04:14:42
I have an iOS app which stores data in NSUserDefults and many other data is set in cache as a result of web view load, social media signing etc. I want to remove all the data from cache created by the app. Is there any way to do this programmatically in iOS? Try NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain]; or [[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]]; You could try this NSUserDefaults NSString *appDomain

NSUserDefaults not present on first run on simulator

北战南征 提交于 2019-12-06 04:06:05
问题 I've got some settings saved in my Settings.bundle, and I try to use them in application:didFinishLaunchingWithOptions , but on a first run on the simulator accessing objects by key always returns nil (or 0 in the case of ints). Once I go to the settings screen and then exit, they work fine for every run thereafter. What's going on? Isn't the point of using default values in the Settings.bundle to be able to use them without requiring the user to enter them first? 回答1: If I got your question

How does NSUserDefaults registerDefaults work? [duplicate]

寵の児 提交于 2019-12-06 03:42:47
问题 This question already has answers here : What is the use of -[NSUserDefaults registerDefaults:]? (5 answers) Closed 6 years ago . When i set registerDefaults in application:didFinishLaunchingWithOptions: i set the default values for the NSUserDefaults throught the app. NSMutableDictionary *defaultsDictionary = [@{@"userHasLoggedInOnce":@NO, @"firstTimeOpeningApp":@YES} mutableCopy]; [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict]; [[NSUserDefaults standardUserDefaults]