[英]When I try to save to ui colors with userDefaults, Xcode gives me the error : Fatal error: Unexpectedly found nil while unwrapping an Optional value
I am trying to allow my app to save UIColors in settings, and when I tried to save the settings for the background color it worked. 我试图允许我的应用将UIColors保存在设置中,当我尝试保存背景色的设置时,它起作用了。 But when I add a second block of code that should allow me to save a second UIColor, it gives me the error - Fatal error: Unexpectedly found nil while unwrapping an Optional value. 但是,当我添加第二个代码块以允许我保存第二个UIColor时,它给了我错误-致命错误:在展开Optional值时意外地找到了nil。 Can someone show me how to save the second UIColor without an error? 有人可以告诉我如何保存第二个UIColor而不出现错误吗?
// First UIColor save - Works
var dd = UIColor(hex: UserDefaults.standard.value(forKey: "TheMainUIColour") as! String )
UserDefaults.standard.set(dd.toHexString, forKey: "TheMainUIColour")
let mainBackgroundColour = UserDefaults.standard.value(forKey: "TheMainUIColour") as! String
let color = UIColor(hex: mainBackgroundColour)
self.view.backgroundColor = dd
// Second UIColor Save - Doesnt Work
let dd2: UIColor = UIColor(hex: UserDefaults.standard.value(forKey: "TheMainUIColour2") as! String )
UserDefaults.standard.set(dd2, forKey: "TheMainUIColour2")
let mainBackgroundColour2 = UserDefaults.standard.value(forKey: "TheMainUIColour2") as! String
let color2 = UIColor(hex: mainBackgroundColour2)
The problem is 问题是
var dd2:UIColor = UIColor(hex: UserDefaults.standard.value(forKey: "TheMainUIColour2") as! String )
it reads from defaults not save , don't use ! 它从默认值读取不保存,请勿使用! to read from defaults as the stored value may be nil , jsut use if let to check the existence of it in defaults or not , if exists use it , if not save the value you want , wjen you open the app again it will read that stored value 从默认值读取,因为存储的值可能为nil,如果让它检查默认值是否存在,则使用jsut;如果存在,请使用它;如果不保存所需的值,则再次打开应用程序时,它将读取储值
if let saved = UserDefaults.standard.string(forKey: "TheMainUIColour2")
{
var dd = UIColor(hex:saved)
}
else
{
print("No saved color")
let dd = UIColor.red
UserDefaults.standard.set(dd.toHexString, forKey: "TheMainUIColour2")
}
Your code crashes because there is nothing set for the key TheMainUIColour2
but you then force-unwrap that nil
value and your app crashes. 您的代码崩溃是因为没有为键TheMainUIColour2
设置任何TheMainUIColour2
但是您随后强行解开了该nil
值,导致应用崩溃。
Your code also makes little sense as written. 您的代码在编写时也没有什么意义。 You (attempt to) load the current value and create a color from it. 您(尝试)加载当前值并从中创建颜色。 You then needlessly write that value back to user defaults. 然后,您无需将该值写回用户默认值。 Then you read the value again as a string. 然后,您再次以字符串形式读取该值。 And then you create the color from the string again. 然后您再次从字符串创建颜色。
Don't do all of those needless extra steps. 不要执行所有这些不必要的额外步骤。 Load the color if it exists and that is it. 加载颜色(如果存在的话)。 This code should not be setting anything. 此代码不应设置任何内容。 You must have other code that decides on what color to use. 您必须具有其他代码来决定使用哪种颜色。 That code should save the color to user defaults. 该代码应将颜色保存为用户默认设置。
if let hexColor = UserDefaults.standard.string(forKey: "TheMainUIColour2") {
let color2 = UIColor(hex: hexColor)
// do something with the color
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.