Getting Optional(“”) when trying to get value from KeyChain

后端 未结 5 1451
自闭症患者
自闭症患者 2020-12-15 23:00

When I try to get my keyChain value, it return a string containing:

Optional(\"[thing in the KeyChain]\")

so, I tried to remove \"Optiona

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 23:36

    You get the Optional("") because the optional value is not unwrapped. You need to put a ! after the object and you won't get the Optional("") bit any more. I would show you the code but you haven't shown us the print() statement. I made some sample ones below that I think would replicate the problem, though I haven't tried them.

    var value:String?
    value = "Hello, World"
    
    print("The Value Is \(value)") // Prints "The Value Is Optional(Hello, World)"
    print("The Value Is \(value!)")// Prints "The Value Is Hello, World"
    

    Im hoping this answers your question or at least points you in the right direction, just ask if you need more information or a better example.

提交回复
热议问题