Swift: shortcut unwrapping of array of optionals

后端 未结 7 911
孤城傲影
孤城傲影 2020-12-03 00:54

Assume we have an array of optionals defined:

var arrayOfOptionals: [String?] = [\"Seems\", \"like\", \"an\", nil, \"of\", \"optionals\"]

I

7条回答
  •  悲&欢浪女
    2020-12-03 01:19

    How about:

    import Foundation
    
    var test: [String!] = ["this","is","a",nil,"test"]
    for string in test {
        if string != nil {
            print(string)
        }
    }
    

    Output is thisisatest.


    In your case use [String!], if I understood you correctly.

提交回复
热议问题