2017-06-05 131 views
-1

我如何在字幕和某些單詞之間的逗號之間添加空格?我使用SWIFT 3.如何在字幕和某些單詞之間的逗號之間添加空格?

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 
} 
+0

我也崩潰了 – Brandon

回答

0

你得到崩潰的原因是因爲你強制包裝可選屬性CLPlacemark,同樣如果你想加入地址嘗試這樣的事情。使String?的數組與您正在嘗試製作地址的所有可選屬性無關,!之後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 
} 
+0

謝謝它的作品...但我怎麼會讓它看起來像這個123例子st。示例城市,CA,示例郵政編碼 – Brandon

+0

我點擊複選標記,對不起,我是本網站的新手。 – Brandon

0

您正在使用的值被迫展開,從而有可能使價值之一是nil由於你越來越當代碼試圖來連接字符串nil崩潰值。

相關問題