使用 Swift 从数组中创建一个用新行 () 分隔的字符串



我在开发iOS应用程序时遇到了一个问题,我试图实现的是从一个字符串数组创建一个字符串。该数组包含给定位置的地址,该地址是使用 CLGeocoder 从反向地理编码中获得的,这是为我提供字符串数组的代码:

let userCoordinates = CLLocation(latitude: mapView.userLocation.location!.coordinate.latitude, longitude: mapView.userLocation.location!.coordinate.longitude)
CLGeocoder().reverseGeocodeLocation(userCoordinates, completionHandler: {
    placemark, error in
    let reverseGeocodedLocation = placemark?.first?.addressDictionary?["FormattedAddressLines"] as? [String]
}

就我而言,reverseGeocodedLocation是:

["Apple Inc.", "Cupertino, CA  95014", "United States"]

结果字符串应分隔数组中的字符串,并以多行格式显示它们,如下所示:

Apple Inc.
Cupertino, CA 95014
United States
我尝试在网上搜索解决方案,

我找到了应该这样做的代码,这可能是解决方案:

print("n".join(reverseGeocodedLocation.map({$0})))

但是编译器说:

无法使用类型为"([字符串]?)"的参数列表调用"join"

if let reverseGeocodedLocation = reverseGeocodedLocation {
    print(reverseGeocodedLocation.joinWithSeparator("n"))
}

作为答案而不是评论。

斯威夫特 3/4:

reverseGeocodedLocation.joined(separator: "n")

最新更新