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

    return result;
}

how cast this array to swift [CGPoint] ?

1 Answer 1

2

You can use UnsafeBufferPointer to cast it as follows

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

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

    free(pointer)

EDIT

If you really need to use C arrays, you may want to rewrite the func method to something like:

- (CGPoint*)pointArray:(NSInteger *)length
{
    int arrSize = 2;
    CGPoint* result = calloc(arrSize, sizeof(CGPoint));
    result[0] = ...
    result[1] = ...

    *length = arrSize;

    return result;
}

Then on the Swift side:

    var arrayLength: Int = 0
    let pointer: UnsafeMutablePointer<CGPoint> = yourInstance.pointArray(&arrayLength)

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

    free(pointer)
Sign up to request clarification or add additional context in comments.

2 Comments

how i can find length of array?
It's a dynamically allocated array, you can't get the length of the array. Pass the length as a parameter of your func() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.