ios - 使用 Swift 从数组中创建一个用换行符 (\n) 分隔的字符串

标签 ios arrays string 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})))

但是编译器说:

Cannot invoke 'join' with an argument list of type '([String]?)'

最佳答案

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

作为回答而不是评论。

swift 3/4:

reverseGeocodedLocation.joined(separator: "\n")

关于ios - 使用 Swift 从数组中创建一个用换行符 (\n) 分隔的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35720185/

相关文章:

ios - iOS ARM 代码可以在模拟器中运行吗?

ios - 在 map 或 reduce 方法的闭包中访问实例变量时可以省略 self 吗?

java - 如何在 zip 中写入字节数组,然后从那里读取它

ruby - 如何检查 Ruby 数组是否包含多个值之一?

c - 解析字符串并将其存储回字符数组时显示随机字节

ios - MPMoviePlayerController 未停留在 UIView 的范围内

ios - 在 Sprite Cocos2d 上添加 box2d Body

SQL 选择从联接返回键控对象

javascript - 按下按钮递增数据然后推送到多维数组

java - 切割字符串java