简体   繁体   中英

Fast method to cast [Float] to [CGFloat]?

I'm having a brain cramp this afternoon. This should be easy.

I did read the docs.

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html

It's easy to convert single instances of Float <> CGFloat but I'm looking for a fast method to cast a LARGE array > 500,000 elements of [Float] to [CGFloat].

var sphereRadiusFloat:[Float] = [0.0,1.0,2.0]
var sphereRadiusCGFloat:[CGFloat] = []

sphereRadiusCGFloat = sphereRadiusFloat as CGFloat

The error is

CGFloat is not convertible to [CGFloat]

I also tried

sphereRadiusCGFloat = CGFloat(sphereRadiusFloat)

which gives error

Could not find an overload operator for 'init' that accepts supplied arguments.

您可以使用map进行以下操作:

sphereRadiusCGFloat = sphereRadiusFloat.map{CGFloat($0)}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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