Helium3 Helium3 - 2 months ago 7
Swift Question

Read a double * in Swift

I have an Obj-C method that returns a

double *
, how is this accessed in Swift as a
Double
?

I get this error

Cannot convert value of type 'UnsafeMutablePointer<Double>' to expected argument type 'Double'


I am calling this
- (double * _Nonnull) modIntensityForDestination:(int) destination;


and failing when I do this

let intensity = audioEngine.modIntensityForDestination(Int32(modDestinationID))


I have tried
withUnsafeMutablePointer
but cant seem to get it working.

Answer

If the method returns a pointer to a single floating point number then you can dereference it with .memory:

let intensity = audioEngine.modIntensityForDestination(...).memory

In Swift 3 it would be .pointee.

Comments