0

我已经尝试加入一些[String!]. 所以我这样

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)

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

4

1 回答 1

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 { }

是一种快速的方法

于 2015-04-02T12:29:54.747 回答