nsuserdefaults

Why NSUserDefaults failed to save NSMutableDictionary in iPhone SDK?

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'd like to save an NSMutableDictionary object in NSUserDefaults . The key type in NSMutableDictionary is NSString , the value type is NSArray , which contains a list of object which implements NSCoding . Per document, NSString and NSArray both are conform to NSCoding . I am getting this error: [NSUserDefaults setObject: forKey:]: Attempt to insert non-property value.... of class NSCFDictionary. Any solution for this? 回答1: I found out one alternative, before save, I encode the root object ( NSArray object) using NSKeyedArchiver , which ends

NSUserDefaults: What's the +resetStandardUserDefaults method good for? How can I provide “system settings”?

匿名 (未验证) 提交于 2019-12-03 01:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want the user to be able to make some preferences like colors, prefered images, etc. When I use NSUserDefaults for this, on first start of the app there will be no preferences, right? So, every time I want to get a preference like NSInteger avatarID = (NSInteger)[[NSUserDefaults standardUserDefaults] objectForKey:@"avatar"]; I have to check if it's null, and then use my system preference. But then I've seen this in the docs: +resetStandardUserDefaults Are there two branches of defaults saved somewhere? The ones from the user, and the ones

How often are NSUserDefaults synchronised?

匿名 (未验证) 提交于 2019-12-03 00:50:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The documentation for NSUserDefaults says that the synchronise method is called periodically but does not mention how frequently. 10 minutes of Google searches did not reveal anything. How frequently is the synchronise method called? 回答1: This is an implementation detail which is not disclosed (probably it isn't even a constant time interval). However, you can find it out yourself by performing a method swizzling on - [NSUserDefaults synchronize] and changing its implementation so that it calls NSLog() every time it's synchronized. 文章来源: How

How do I find out the current keyboard used on iOS8?

﹥>﹥吖頭↗ 提交于 2019-12-02 20:42:28
You can get a list of the keyboards installed on the iOS device using: NSUserDefaults *userDeafaults = [NSUserDefaults standardUserDefaults]; NSDictionary * userDefaultsDict = [userDeafaults dictionaryRepresentation]; NSLog(@"%@", userDefaultsDict); This yields something in the console like: { ... AppleKeyboards = ( "en_US@hw=US;sw=QWERTY", "es_ES@hw=Spanish - ISO;sw=QWERTY-Spanish", "emoji@sw=Emoji", "com.swiftkey.SwiftKeyApp.Keyboard" ); AppleKeyboardsExpanded = 1; ... } This tells me that the device has the Spanish, Emoji and SwiftKey keyboards installed, but it tells me nothing about which

NSUserDefaults in Swift - implementing type safety

穿精又带淫゛_ 提交于 2019-12-02 19:37:45
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 be accepted and when getting a value the result

Save UIImage array in NSUserDefaults

家住魔仙堡 提交于 2019-12-02 18:04:28
问题 I want to put a UIImage array into UserDefaults and then retrieve it. After that set the retrieved images in an image view. How can I do that? 回答1: You can easily save your image in documentDirectory as below: let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first let fileURL = documentDirectory?.appendingPathComponent(yourImageName) and then, you can give your image to UserDefaults 回答2: First of all that is not ideal way to save image in user

Why are NSUserDefaults not read after flat battery IOS7

房东的猫 提交于 2019-12-02 17:20:57
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 3GS on IOS6 which does not exhibit the same behaviour. I suspect it may be a similar issue to the one

NSArrayURL , userDefault filePath and URL

杀马特。学长 韩版系。学妹 提交于 2019-12-02 17:17:24
问题 I have one pdf source code and I want to add the Url in Array and use UserDefault let defaults = UserDefaults.standard struct Constants { static let myKeyURL = "myKeyUrl" } I download the Pdf Like This let documentsPath =NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let fileName = urlString as NSString; let filePath="\(documentsPath)/\(fileName.lastPathComponent)"; After I save the Path like This var arrayUrl = [String]() arrayUrl.append(filePath) self

Attempt to insert non-property value Objective C

天涯浪子 提交于 2019-12-02 16:20:53
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:@"restoAbout"]; [dic setObject:resto.restoAddress forKey:@"restoAddress"]; [dic setObject:resto

NSUserDefaults removeObjectForKey vs. setObject:nil

十年热恋 提交于 2019-12-02 16:02:11
Are the following two lines equivalent? 1. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"example key"] 2. [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"example key"] 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 the following code: UserDefaults.standard.set(["a", "b", "c"], forKey: "MyDefaults") print("Test A: My