μολὼν.λαβέ
μολὼν.λαβέ

Reputation: 668

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.

Upvotes: 1

Views: 2459

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236370

You can use map to do it as follow:

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

Upvotes: 4

Related Questions