How to go from CMutablePointer to CGFloat[] in Swift

前端 未结 2 1822
抹茶落季
抹茶落季 2020-12-24 09:23

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...



        
2条回答
  •  清酒与你
    2020-12-24 09:47

    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   => const Type * 
    CMutablePointer => Type *
    

    Pointers as Return Types, Variables, and Arguments*

    COpaquePointer      => void *
    UnsafePointer => 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 * const *
    CMutablePointer            => Type * __strong *
    AutoreleasingUnsafePointer => Type **
    

    Swift Pointers

    When using the CConstPointer pointer in Swift, you may pass any one of these:

    • nil, which will be evaluated as a NULL pointer
    • A CConstPointer value
    • A CConstVoidPointer value
    • A CMutablePointer value
    • A CMutableVoidPointer
    • A AutoreleasingUnsafePointer value which will be converted to CConstPointer 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 pointer in Swift, you may pass any one of these:

    • nil, which will be evaluated as a NULL pointer
    • A CMutablePointer 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 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?)

提交回复
热议问题