Swift 如何在副标题之间添加空格,在某些单词之间添加逗号?
我如何在副标题之间加一个空格,在一些单词之间加一个逗号?我用的是swift 3Swift 如何在副标题之间添加空格,在某些单词之间添加逗号?,swift,xcode,swift3,xcode8,Swift,Xcode,Swift3,Xcode8,我如何在副标题之间加一个空格,在一些单词之间加一个逗号?我用的是swift 3 override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! let selectedItem
override
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = selectedItem.subThoroughfare! + selectedItem.thoroughfare!
+ selectedItem.locality! + selectedItem.administrativeArea! + selectedItem.postalCode!
return cell
}
您正在对值使用强制展开,其中一个值可能是
nil
,因此当代码试图将字符串连接到nil
值时,会发生崩溃。导致崩溃的原因是,您正在强制包装CLPlacemark
的可选属性,如果你想加入地址,也可以尝试这样的方法。使用当前尝试在没有的情况下生成地址的所有可选属性生成字符串的数组?
之后,flatMap
数组忽略nil
,然后简单地用分隔符,
加入数组
override public tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
let addressArray = [selectedItem.subThoroughfare, selectedItem.thoroughfare, selectedItem.locality, selectedItem.administrativeArea, selectedItem.postalCode].flatMap({$0})
if addressArray.isEmpty {
cell.detailTextLabel?.text = "N/A" //Set any default value
}
else {
cell.detailTextLabel?.text = addressArray.joined(separator: ", ")
}
return cell
}
我也收到了一封信谢谢你它很有效。。。但是我如何让它看起来像这个123示例加利福尼亚州圣范例城示例邮编我点击复选标记,对不起,我是这个网站的新手。