nsuserdefaults

How to save a color in NSUserDefaults in swift?

半城伤御伤魂 提交于 2019-12-03 07:36:44
I have a litle problem with saving a color in NSUserDefaults . In objective-c is easy : -(NSColor *)colorForKey:(NSString *)key{ NSData *data; NSColor *color; data = [[NSUserDefaults standardUserDefaults] objectForKey:key]; color= [NSUnarchiver unarchiveObjectWithData:data]; if( ! [color isKindOfClass:[NSColor class]] ){ color = nil; } return color; } -(void)setColor:(NSColor *)color forKey:(NSString *)key{ NSData *data = [NSArchiver archivedDataWithRootObject:color]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:key]; } The above code works fine in objective-c, but when i try

Save and Load UISwitch state

[亡魂溺海] 提交于 2019-12-03 05:06:46
I want to save the swtich state and load it when the program restarted. [[NSUserDefaults standardUserDefaults] setBool:switchControl.on forKey:@"switch"]; [[NSUserDefaults standardUserDefaults] synchronize]; This is the saving part BOOL test= [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"]; NSLog(@"%@",test?@"YES":@"NO"); if(test == YES) [switchControl setOn:YES animated:YES]; else [switchControl setOn:NO animated:YES]; This is the part that is need to be set the switch to its value.I did this in viewdidload method because when i close the application and restarted it i want the

Setting Up HighScore in Swift and Sprite Kit using NSuserdefaults

别来无恙 提交于 2019-12-03 04:03:58
I am building a swift game and a need to set up a highscore , i have been trying to figure out how to do it for a while. So please can you tell me how to search this library to see if there is a already saved high score and then compare it to the user's score. I have tried coming out with this but it won't work. Please Help and be as clear as possible. Giving me example code would be extremely appreciated. if let highscore: AnyObject = NSUserDefaults.valueForKey("highscore") { var savedScore: Int = NSUserDefaults.standardUserDefaults().objectForKey("highScore") as Int if ( savedScore < Score)

Why are NSUserDefaults not read after flat battery IOS7

蓝咒 提交于 2019-12-03 03:58:31
问题 I am using NSUserDefaults to store whether the app EULA and PP have been accepted (among other things) This works fine in general. I can start, exit then return to the app and it reads the value fine. I can kill the app and restart - reads the defaults fine. I can restart the phone, then restart the app and it reads the defaults fine. But when the phone restarts from a flat battery, I open the app and am prompted to accept my EULA an PP again. This only happens on my iPhone5 on IOS7. I have a

IOS: store an array with NSUserDefault

[亡魂溺海] 提交于 2019-12-03 03:54:01
I want to store an array with NSUserDefault , then, I put in applicationDidEnterBackground [[NSUserDefaults standardUserDefaults] setObject:myArray forKey:@"myArray"]; and in application didFinishLaunchingWithOption myArray= [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"myArray"]]; it's ok for multitasking device, but for not-multitasking device, how can I solve? Store the object in NSUserDefaults in -applicationWillTerminate: , if it hasn't already been saved by the invocation of -applicationDidEnterBackground: (i.e. check if multitasking is

Attempt to insert non-property value Objective C

这一生的挚爱 提交于 2019-12-03 02:50:42
问题 Hi l am trying to create a fourates lists from an restaurants Object, my application has a list of different restaurants, l want the ability for users to add favourate restaurants, and this code is not working - (IBAction)toggleFav:(id)sender { Restaurant *resto = [self restaure]; NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; [dic setObject:resto.price forKey:@"restoPrice"]; [dic setObject:resto.restaurantId forKey:@"restaurantId"]; [dic setObject:resto.restoAbout forKey:@

NSUserDefaults in Swift - implementing type safety

匿名 (未验证) 提交于 2019-12-03 02:45:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: One of the things that bugs me about Swift and Cocoa together is working with NSUserDefaults, because there is no type information and it is always necessary to cast the result of objectForKey to what you are expecting to get. It is unsafe and impractical. I decided to tackle this problem, making NSUserDefaults more practical in Swift-land, and hopefully learning something along the way. Here were my goals in the beginning: Complete type safety: each key has one type associated with it. When setting a value, only a value of that type should

NSUserDefaults removeObjectForKey vs. setObject:nil

孤者浪人 提交于 2019-12-03 02:30:51
问题 Are the following two lines equivalent? 1. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"example key"] 2. [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"example key"] 回答1: Swift 3.0 The below answer is no longer the case when I tested this. When set to nil the result is NSCFData being stored. Possibly an NSNull object reference, but I am not positive. To completely remove a value for a key use UserDefaults.standard.removeObject(forKey: "YourDefault") I tested with

NSUserDefaults Custom object - Property list invalid for format: 200 (property lists cannot contain objects of type &#039;CFType&#039;)

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I must be incorrectly using Custom Objects for NSUserDefaults. The error " Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')". Below is my code, the Goal class is of particular interest, since this is where I am adopting the NSCoding protocol. This code is global. func saveGoals (goals : [Goal]) { var updatedGoals = NSKeyedArchiver.archivedDataWithRootObject(goals) NSUserDefaults.standardUserDefaults().setObject(updatedGoals, forKey: "Goals") NSUserDefaults.standardUserDefaults().synchronize() }

NSUserDefaults synchronize-method

匿名 (未验证) 提交于 2019-12-03 02:13:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:@"xxxxxxxx" forKey:@"name"]; [defaults synchronize]; I need to know why do i have to use the last line of the above code [defaults synchronize] ? What is the purpose of using it? Is it a must ? 回答1: The purpose of [default synchronize]; is to make the user defaults get written on disk immediately . You don't need to call it explicitly, iOS already does it at appropriate moments. So you can remove that line. In fact, it's a performance problem if you call