简体   繁体   English

在Swift中将String数组转换为CGFloat数组

[英]Converting String array to CGFloat array in Swift

Here is my String array: 这是我的String数组:

let StringArray = ["1.4", "75.3", "23.4"]

And i wan't to convert this String Array to a CGFloat Array how can i do that? 而且我不会将此字符串数组转换为CGFloat数组,我该怎么做?

The easiest way is to use map to transform each element from one type to another type: 最简单的方法是使用map将每个元素从一种类型转换为另一种类型:

let doubleArray = StringArray.map {
    CGFloat(($0 as NSString).doubleValue)
}

The cast to NSString is needed because there's no string to double conversion in plain swift - at least I am not aware of any. 需要NSString转换为NSString ,因为没有字符串可以快速地进行双重转换-至少我什么都不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM