ios - Swift,使用连接器连接(隐式展开)可选字符串

标签 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)

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

最佳答案

如果你总是知道它们可以被展开,那么你就可以很容易地加入字符串

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

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

if a.isEmpty == false { }

是一个快速的方法来做到这一点

关于ios - Swift,使用连接器连接(隐式展开)可选字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29410232/

相关文章:

objective-c - UISplitViewController 重新加载数据

ios - 当我在 TextField 中触摸时,如何在 Xcode 项目中禁用键盘出现?

ios - 创建像 iOS 日历一样的 TableView

SQL:获取从左连接返回的行数

java - lg(N) 时间内的 AVL 树连接操作算法或伪代码

ios - 当在 DetailViewController 中显示时,TableView 的自定义 UITableViewCell 消失

ios - 您如何以编程方式检索iOS照片流中对照片发表的评论?

ios - Google API 获取天气信息

ios - 同时使用不同的动画曲线对自动布局约束进行动画处理

postgresql - 如何解释 Postgresql 中 SELECT...JOIN...INTO 的结果?