How to go from CMutablePointer<CGFloat> to CGFloat[] in Swift
swift
Solution
EDIT: Just wanted to clarify a few things after learning a bit more from the online documentation (PDF).
There are a few commonly used pointer types in Swift, here is how they map to C equivalents:
Pointers as Arguments
CConstVoidPointer => const void *
CMutableVoidPointer => void *
CConstPointer<Type> => const Type *
CMutablePointer<Type> => Type *
Pointers as Return Types, Variables, and Arguments*
COpaquePointer => void *
UnsafePointer<Type> => Type *
NOTE: Arguments follow this rule only when they are more than one pointer level deep, otherwise see above.
Pointers for Class Types
CConstPointer<Type> => Type * const *
CMutablePointer<Type> => Type * __strong *
AutoreleasingUnsafePointer<Type> => Type **
Swift Pointers
When using the `CConstPointer<Type>` pointer in Swift, you may pass any one of these:
- `nil`, which will be evaluated as a `NULL` pointer
- A `CConstPointer<Type>` value
- A `CConstVoidPointer` value
- A `CMutablePointer<Type>` value
- A `CMutableVoidPointer`
- A `AutoreleasingUnsafePointer<Type>` value which will be converted to `CConstPointer<Type>` if necessary
- A `Type` value passed by address (`&` operator)
- A `Type[]` array
NOTE:`CConstVoidPointer` can take any of the above values as well.
When using the `CMutablePointer<Type>` pointer in Swift, you may pass any one of these:
- `nil`, which will be evaluated as a `NULL` pointer
- A `CMutablePointer<Type>` value
- A `Type` value passed by address (`&` operator)
- A `Type[]` array passed by address (`&` operator)
NOTE:`CMutableVoidPointer` can take any of the above in addition to `CMUtableVoidPointer` values.
So it would seem in your case that a `CMutablePointer<CGFloat>` could also be a pointer to an array of `CGFloat` values. Though I am not completely sure how to dereference that in Swift. (Perhaps the `as` operator?)
Problem
I am trying to convert an ObjC class that uses Facebook's pop library to Swift. Pop uses quite a bit of C. In ObjC, I have a block that looks like this... ``` prop.readBlock = ^(SHVGraphViewObjc *graphView, CGFloat values[]) { values[0] = [graphView.centerOffsets[idx] doubleValue]; }; ``` The equivalent closure definition in Swift would be `prop.readBlock = {(graphView: AnyObject!, values: CMutablePointer<CGFloat>) in }` What I can't figure out is how to convert the `values[0] = [graphView.centerOffsets[idx] doubleValue];` to Swift? How do I let Swift know that `CMutablePointer<CGFloat>` should be a `CGFloat[]`?