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

前端 未结 6 1791
遥遥无期
遥遥无期 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条回答
  •  感动是毒
    2020-12-10 02:33

    Swift 3.1

    From Swift 3, you can use String(describing:) to print out optional value. But the syntax is quite suck and the result isn't easy to see in console log.

    So that I create a extension of Optional to make a consistent nil value.

    extension Optional {
        var logable: Any {
            switch self {
            case .none:
                return "⁉️" // Change you whatever you want
            case let .some(value):
                return value
            }
        }
    }
    

    How to use:

    var a, b: Int?
    a = nil
    b = 1000
    print("a: ", a.logable)
    print("b: ", b.logable)
    

    Result:

    a: ⁉️
    b: 1000
    

提交回复
热议问题