How do I prevent my text from displaying Optional() in the Swift interpolation?

前端 未结 3 852
星月不相逢
星月不相逢 2021-01-28 09:24

How do I prevent my text from displaying Optional() in the Swift interpolation?

My text displaying is :

---You can only switch properties once all images from Op

3条回答
  •  难免孤独
    2021-01-28 09:56

    Swift is doing this because you provided an optional string, not a string. To solve it you need to unwrap the optional.

    You can either use ! to unwrap an optional string, such as:

    messageText.text = "You can only switch properties once all images\(propertyNameStr!) have finished uploading."
    

    Or you can use a if statement to unwrap the optional.

    if let nameString = propertyNameStr {
        messageText.text = "You can only switch properties once all images\(nameString) have finished uploading."
    }
    

提交回复
热议问题