Swift,Joining(Implicitly Unwrapped)可选字符串和一个木匠

时间:2015-04-02 10:05:33

标签: ios swift join forced-unwrapping

我试图加入一些[String!]。所以我正在制作this

extension String {
    func join(strings: String!... ) -> String {
        return self.join(strings.filter({ $0?.isEmpty == false }).map {$0 as String})
    }
}

var country: String! = nil
var city: String! = ""
var address: String! = "Nowon"
var detailAddress: String! = "Gongneung-dong"

let combinedAddress = " ".join(country, city, address, detailAddress)

但是,它似乎仍然很复杂。我如何优化这个?

1 个答案:

答案 0 :(得分:1)

如果你总是知道它们可以被解包,那么你可以轻松地加入字符串

var a = "this is a "
var b = "this is b "
var c = "this is c"
let myString = a + b + c

我在我的代码中做了类似的事情,但首先我检查字符串是否为空

if a.isEmpty == false { }

快速做到这一点