转换字符串数组CGFloat的数组中的斯威夫特 [英] Converting String array to CGFloat array in Swift

查看:95
本文介绍了转换字符串数组CGFloat的数组中的斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的字符串数组:

Here is my String array:

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

和我wan't这个字符串数组转换为CGFloat的阵列我该怎么办呢?

And i wan't to convert this String Array to a CGFloat Array how can i do that?

推荐答案

最简单的方法是使用地图来的每个元素从一种类型转变为另一种类型:

The easiest way is to use map to transform each element from one type to another type:

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

演员到的NSString 需要,因为没有字符串以纯SWIFT双变换 - 至少我不知道任何

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.

这篇关于转换字符串数组CGFloat的数组中的斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆