如何不从ViewDidLoad中的NSUserDefaults获取零值 [英] how to don't get a nil value from NSUserDefaults in ViewDidLoad

查看:66
本文介绍了如何不从ViewDidLoad中的NSUserDefaults获取零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,当我的viewControlerviewDidLoad开始时,我必须使用NSUserDefaults.standardUserDefaults()加载某些数据,而该数据在此监视中不存在.当我在同一viewController中点击send Button时,将保存这些数据,而当我再次打开此viewController时,则需要此数据.现在看起来像这样:

I have a case where when my viewControler starts in viewDidLoad I have to load some data using NSUserDefaults.standardUserDefaults() which doesn't exist in this monent. This data are saved when I tap send Button in the same viewController and I need this data when I open this viewController again. Now it looks like that:

var orderHistory = [String:String]()    

vievDidLoad(){    

let userDefault = NSUserDefaults.standardUserDefaults()
let orderHistory = userDefault.objectForKey("orderHistory")

if orderHistory == nil {
self.orderHistory = orderHistory["name":"", "surname":""] as! [String:String]
} else {
self.orderHistory = orderHistory as! [String:String]
{

}// end viewDidLoad

此刻,我收到一个信息,我的记忆有问题.我应该如何避免这种情况?

In this moment I recieve an imformation, I have a problem with memory. How should I avoid this situation?

推荐答案

正如Leo Dabus所说,您应该尝试使用??.无合并运算符. ObjectForKey不提供默认值,因为在您第一次设置它之前,它不知道它是什么类型的对象.如果您尝试不设置一次就访问它的值,则会导致nil崩溃.

As Leo Dabus said you should try using the ?? nil coalescing operator. ObjectForKey does not provide a default value because it doesnt know what kind of object it is until you set it the first time. This results in a nil crash if you try to access it value without having it set once.

将其与"boolForKey"进行比较,您不必这样做,因为它知道您正在处理布尔值,因此默认自动为false.

Compare this to say "boolForKey" where you dont have to do this, because it knows you are dealing with boolean values and therefore defaults to false automatically.

您也不必创建2个orderHistory字典,这只会使您的代码更加混乱.

You also dont have to create 2 orderHistory dictionaries, it just makes your code more confusing.

试试看

var orderHistory = [String:String]()    

 vievDidLoad(){    

 let userDefault = NSUserDefaults.standardUserDefaults()
 orderHistory = userDefault.objectForKey("orderHistory") as? [String: String] ?? orderHistory 


   //than just use the 1 dictionary without the if statements or creating another one.


  }// end viewDidLoad

您检查是否存在已保存的数据(如[String:String])并相应地更新字典.如果不存在保存的数据,它将使用orderHistory(?? orderHistory)的默认值,在您的情况下为空字典. 这样,您就不必做nil检查,所有检查都在那一行中完成.

You check if saved data exists (as? [String: String]) and update the dictionary accordingly. If no saved data exists it will use the default values in orderHistory (?? orderHistory), which in your case is an empty dictionary. This way you dont have to do a nil check, its all done in that one line.

也请尝试将密钥放入结构或全局文件中,以免造成输入错误.我看到人们一直没有这样做,这是非常糟糕的做法. 因此,例如,在您的课程上方创建一个结构

Also try putting your keys into structs or global files so that you avoid typos. I see people not doing this all the time and its really bad practice. So for example, above your class create a struct

struct Key {
     static let orderHistory = "OrderHistory"
}

并像这样使用它

 ...objectForKey(Key.orderHistory)

这篇关于如何不从ViewDidLoad中的NSUserDefaults获取零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆