nsuserdefaults

iOS: Should I use Core Data or NSUserDefaults?

点点圈 提交于 2019-12-08 04:41:38
问题 Background: I have an app that retrieves a list of restaurants from a database, each have an individual array of basic information about them (hours, name, address, etc). I would like to retrieve that information from the server on the apps first load, but then have it stored within the app itself, with either NSUserDefaults or Core Data, since the information is unlikely to change. The max number of restaurants I would be storing is about 25, is that a small enough data collection to use

iPhone - User Defaults and UIImages

时光怂恿深爱的人放手 提交于 2019-12-08 02:45:04
问题 I've been developing an iPhone app for the last few months. Recently I wanted to up performance and cache a few of the images that are used in the UI. The images are downloaded randomly from the web by the user so I can't add specific images to the project. I'm also already using NSUserDefaults to save other info within the app. So now I'm attempting to save a dictionary of UIImages to my NSUserDefaults object and get... -[UIImage encodeWithCoder:]: unrecognized selector sent to instance I

Strange NSUserDefaults behavior

ⅰ亾dé卋堺 提交于 2019-12-08 01:36:19
问题 While my iPhone app is running I store some data (NSStrings) in the NSUserDefaults. I'm storing the high score for a game just fyi. When I double-tap the home button and kill the app the values I store in NSUserDefaults are often not there when I restart the app. Why is this happening? The code I'm using was working fine in all the pre-multitasking OSs (3.0 etc). From reading/searching the net it looks like doing [NSUserDefaults standardUserDefaults] synchronize]; could help. Any thoughts?

Swift NSUserDefaults NSArray using objectForKey

亡梦爱人 提交于 2019-12-08 01:04:36
问题 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.

Working With Structs, Arrays, and UserDefaults (Swift)

最后都变了- 提交于 2019-12-07 20:27:06
问题 I am creating an iOS app using swift where a user can play music. I have created a struct and an array of the songs that the app offers in a .swift file. struct Song { var title: String var artist: String var lyrics: String } var songs: [Song] = [ Song(title: Song One Title", artist: "Song One Artist", lyrics: ""), Song(title: Song Two Title", artist: "Song Two Artist", lyrics: "Song Two Lyrics"), Song(title: Song Three Title", artist: "Song Three Artist", lyrics: ""), ]; The array songs

How to save My Data Type in NSUserDefault?

穿精又带淫゛_ 提交于 2019-12-07 20:06:33
问题 I want to save My Data Type temporary in NSUserDefault. this data later pull back. I tried to following code. MyObject *myObject = [[MyObject alloc] init]; [[NSUserDefaults standardUserDefaults] setObject:myObject forKey:@"kMyObject"]; but console show to text me following message. -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<MyObject: 0x4aadb0>' of class 'MyObject'. Note that dictionaries and arrays in property lists must also contain only property values. I

Saving map pins to array, Swift

荒凉一梦 提交于 2019-12-07 18:45:46
问题 I am creating a map type application, once the user presses a button a pin is dropped to the map at their current location. I am trying to save map pins to an array so that they remain once the app is closed. Here is my code so far: func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.last let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) let region =

Getting (default) value from settings bundle

爷,独闯天下 提交于 2019-12-07 17:15:47
问题 I have an IOS5 project. I have added a settings bundle to my project and added some settings to it. I set properties of 'host_ip': <dict> <key>AutocapitalizationType</key> <string>None</string> <key>AutocorrectionType</key> <string>No</string> <key>DefaultValue</key> <string>http://localhost</string> <key>IsSecure</key> <false/> <key>Key</key> <string>host_ip</string> <key>KeyboardType</key> <string>URL</string> <key>Title</key> <string>Host</string> <key>Type</key> <string

Unable to Write Swift Dictionary to NSUserDefaults in Beta 5

∥☆過路亽.° 提交于 2019-12-07 15:50:19
问题 I have been working on an app in which I user NSUserDefaults to persist a Dictionary for use elsewhere in the program. This been working perfectly since Beta 1. Now with the latest update (beta 5) this no longer works. It appears they have removed the ability to persist a swift Dictionary in this manner. However, I can persist an NSDictionary. Here is a code snippet that worked 2 days ago and now is broken. var userDefaults = NSUserDefaults.standardUserDefaults() userDefaults.setObject

Observing value changes to an NSUserDefaults key

南楼画角 提交于 2019-12-07 14:01:10
问题 I'm interested in the value change of a particular key which I keep in NSUserdefaults. However, what I have is not working for me. observeValueForKeyPath does not get triggered. Update: I think I've discovered the issue. Rather than using a defined constant, if I use a string then it gets fired. [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:kSomethingInteresting options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil]; } - (void