-(CGPoint*)func
{
    CGPoint* result = calloc(2, sizeof(CGPoint));
    result[0] = ..;
    result[1] = ..;

    return result;
}

如何将此数组转换为swift[CGPoint]?

最佳答案

您可以使用UnsafeBufferPointer将其投射如下

    let pointer: UnsafeMutablePointer<CGPoint> = yourInstance.func()

    let swiftArray = Array(UnsafeBufferPointer(start: pointer, count: 2))

    free(pointer)

编辑
如果您确实需要使用C数组,您可能需要将func方法重写为如下内容:
- (CGPoint*)pointArray:(NSInteger *)length
{
    int arrSize = 2;
    CGPoint* result = calloc(arrSize, sizeof(CGPoint));
    result[0] = ...
    result[1] = ...

    *length = arrSize;

    return result;
}

那么在斯威夫特方面:
    var arrayLength: Int = 0
    let pointer: UnsafeMutablePointer<CGPoint> = yourInstance.pointArray(&arrayLength)

    let swiftArray = Array(UnsafeBufferPointer(start: pointer, count: arrayLength))

    free(pointer)

关于objective-c - objc结构数组快速数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33625929/

10-15 06:38