nsuserdefaults

Storing object on iOS - best solution?

半城伤御伤魂 提交于 2019-12-01 22:59:28
问题 I'm building an app for a blog where a user can save their favorite posts. When they do, I want to store my object which contains: the post's URL, title and image URL. Should I go for UserDefaults (formerly NSUserDefaults ) or start with Core Data right away? 回答1: Should I go for User Defaults or start with Core Data right away? There are more possibilities here: you can also use plain files or plists, or use sqlite without Core Data. The answer depends on the number of items that you plan to

Swift optionals - warning on conditional cast from 'x' to 'x' always succeeds

℡╲_俬逩灬. 提交于 2019-12-01 20:47:40
I was wondering if there is a way to turn off/avoid 'yellow' warnings in xcode on if let...NSUserDefaults constructs where the key is of a known value. For example: if let x = NSUserDefaults.standardUserDefaults().integerForKey("myKey") as? Int {...} Because of the if let I have to use as? . However, as I am using a known value type (in this case integer) the as? Int is effectively redundant - which is why I am getting the 'yellow warning'. Thoughts? Is there a better way to code these types of constructs? My suggestion would be to address the issue instead of silencing the warnings. :)

NSUserDefault with App Group is not working in iOS 8 Beta3

 ̄綄美尐妖づ 提交于 2019-12-01 20:36:19
I have to save Boolean Value to NSUserDefault in my App with custom keyboard extension and share with App Group. My Code is worked in iOS 8 Beta1. self.defaults = [NSUserDefaults standardUserDefaults]; if([self.defaults boolForKey:@"BlackKey"]) { NSLog(@"Black"); } else { NSLog(@"White"); } But Not in iOS 8 Beta3. When i retrieve Boolean value from NSUserDefault , it's return nothing and i can't load from custom keyboard extension. I have also tried with initWithSuiteName in NSUserDefault . Am i only one for that problem or bugs of iOS 8 Beta3? A few probable solutions are: Your app group is

UI_19 数据持久化(本地存储)

陌路散爱 提交于 2019-12-01 19:24:36
一、数据持久化概述 数据持久化就是数据的永久存储。其本质是将数据保存为文件,存到程序的沙盒中。 1、 数据持久化的方式 1.1 writeToFile:简单对象写入文件 1.2 NSUserDefaults: 应用程序偏好设置 1.3 Sqlite: 轻量级关系型数据库,不能直接存储对象 (NSData 除外 ) ,需要用到一些 SQL 语句,先将复杂对象归档 ( 对象 ->NSData) 1.4 CoreData: 对象型数据库,实质是将数据库的内部存储细节封装 1.5 Plist 文件 2、 应用程序沙盒 每一应用程序都有自己的应用沙盒 ,沙盒的本质就是一个文件夹,名字是随机分配的。 与其他应用程序沙盒隔离,应用程序本身只能访问自己沙盒的数据。 (iOS8+ 对沙盒之间的访问部分开放 ) 2.1 应用程序包 (.app) 包含了应用程序中所用到的所有资源文件和可执行文件 (Base on Unix)。iOS8时,app不存储在沙盒中,有单独的文件夹存储所有程序的app包。 2.2 HomeDirectory Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录 tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件

iOS - setObject vs. Synchronize

筅森魡賤 提交于 2019-12-01 18:34:14
Newbie question. If I set an object into NSUserDefault do I also need to Synchronize to be sure the object goes back into NSUserDefault database permanently? I guess what I am asking is, is "setobject" placing the object right back into the database permanently or into some temporary storage that needs to be synchronized to ensure permanent storage? For example: //below I place a new copy of a list in NSUserDefaults after adding a row of data userDefaults.setObject(newMutableList, forKey: "itemList") //do I also need to synchronize right away to be sure database updates permanently?

NSUserDefaults: Is it possible to get userDefaults from another app?

こ雲淡風輕ζ 提交于 2019-12-01 17:46:45
There are two apps installed on iPhone (myApp and competitorApp).competitorApp is saving user settings with using NSUserDefaults. I know all keys (value forKey) which using competitorApp. Is it possible to get values in myApp from competitorApp? In the iOS App Programming Guide, in the Security section you can read: For security reasons, iOS places each app (including its preferences and data) in a sandbox at install time. A sandbox is a set of fine-grained controls that limit the app’s access to files, preferences, network resources, hardware, and so on. As part of the sandboxing process, the

NSUserDefaults. setValue works, Not setBool

人盡茶涼 提交于 2019-12-01 16:18:47
I trying to store some settings in NSUserDefaults, but It seems that the app won't store the setBool values. This works: [[NSUserDefaults standardUserDefaults] setValue: @"hello" forKey: @"test"]; [[NSUserDefaults standardUserDefaults] synchronize]; When I terminate the app and restart it, the value have been saved. However, when I do this: [[NSUserDefaults standardUserDefaults] setBool: YES forKey: @"test"]; [[NSUserDefaults standardUserDefaults] synchronize]; It won't save after I close the app and restart it. Should I file a bug report, or is there something I'm missing here? Thanks Edit: I

NSUserDefaults. setValue works, Not setBool

纵饮孤独 提交于 2019-12-01 15:10:04
问题 I trying to store some settings in NSUserDefaults, but It seems that the app won't store the setBool values. This works: [[NSUserDefaults standardUserDefaults] setValue: @"hello" forKey: @"test"]; [[NSUserDefaults standardUserDefaults] synchronize]; When I terminate the app and restart it, the value have been saved. However, when I do this: [[NSUserDefaults standardUserDefaults] setBool: YES forKey: @"test"]; [[NSUserDefaults standardUserDefaults] synchronize]; It won't save after I close the

Accessing UserDefault Array URL to Populate CollectionView

馋奶兔 提交于 2019-12-01 14:43:06
This is a follow-up from a previous post here . I am now trying to access the array and use it to populate my collectionView. When I try the following code, my imageData comes back as nil and I get a crash, even though the imageURL correctly shows as a filepath. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let usD = UserDefaults.standard let array = usD.stringArray(forKey: "WeatherArray") ?? [] let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! myCell let myString = array

Saving array using NSUserDefaults crashes app

倖福魔咒の 提交于 2019-12-01 14:40:27
I feel as if I am doing things correctly, but I am getting an error at the end of my data conversion and retrieval. Please see the code below: class Task:NSObject, NSCoding { var name:String var notes:String var date:NSDate var taskCompleted:Bool init(name:String, notes:String,date:NSDate, taskCompleted:Bool){ self.name = name self.notes = notes self.date = date self.taskCompleted = taskCompleted } required init(coder decoder: NSCoder){ self.name = (decoder.decodeObjectForKey("name") as! String?)! self.notes = (decoder.decodeObjectForKey("notes") as! String?)! self.date = (decoder