简体   繁体   English

当我尝试使用userDefaults保存为ui颜色时,Xcode给我以下错误:致命错误:在展开可选值时意外发现nil

[英]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.

相关问题 尝试获取联系电话时,出现错误“致命错误:在展开可选值时意外发现nil” - I got an error “fatal error: unexpectedly found nil while unwrapping an Optional value” when I try to get a phone number in contact 尝试添加脉冲时,Xcode始终返回“致命错误:在展开可选值时意外发现nil” - Xcode keeps returning “fatal error: unexpectedly found nil while unwrapping an Optional value”, when attempting to add an impulse 在项目中添加了自定义字体,但是当我尝试使用它们时,出现“严重错误:在展开可选值时意外发现nil” - Added custom font to project but when I try and use them I get a “fatal error: unexpectedly found nil while unwrapping an Optional value” 线程1:致命错误:当我尝试使用NSuserDefaults保存对象时,在展开可选值时意外发现nil - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value as I am trying to save an object with NSuserDefaults 解包可选的 IBOutlet 时出现错误(线程 1:致命错误:解包可选值时意外发现 nil) - I have an error when unwrapping an optional IBOutlet (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value) 当我实现Parse时,我得到“致命错误:在展开可选值时意外发现nil” - When I implement Parse I get “fatal error: unexpectedly found nil while unwrapping an Optional value” Xcode Swift:appDelgate.window! is nil :致命错误:在解开可选值时意外发现 nil - Xcode Swift : appDelgate.window! is nil : Fatal error: Unexpectedly found nil while unwrapping an Optional value ARQuicklook-致命错误:展开一个可选值时意外发现nil - ARQuicklook - Fatal error: Unexpectedly found nil while unwrapping an Optional value 致命错误:在AVFoundation中解开Optional值时意外发现nil - fatal error: unexpectedly found nil while unwrapping an Optional value in AVFoundation 致命错误:展开一个可选值NSURL时意外发现nil - Fatal Error: Unexpectedly found nil while unwrapping an Optional value NSURL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM