How to print a string from plist without “Optional”?

前端 未结 6 1792
遥遥无期
遥遥无期 2020-12-10 01:46

I load a value from a dictionary in a plist but when I print it to the console, it prints: Optional(Monday Title) rather than just \"Monday Title\".

How can I get ri

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 02:31

    With some try, I think this way is better.

    (variableName ?? "default value")!
    

    Use ?? for default value and then use ! for unwrap optional variable.

    Here is example,

    var a:String? = nil
    var b:String? = "Hello"
    
    print("varA = \( (a ?? "variable A is nil.")! )")
    print("varB = \( (b ?? "variable B is nil.")! )")
    

    It will print

    varA = variable A is nil.
    varB = Hello
    

提交回复
热议问题