[英]objc struct array to swift array
-(CGPoint*)func
{
CGPoint* result = calloc(2, sizeof(CGPoint));
result[0] = ..;
result[1] = ..;
return result;
}
how cast this array to swift [CGPoint] ? 如何将此数组快速转换为[CGPoint]?
You can use UnsafeBufferPointer to cast it as follows 您可以使用UnsafeBufferPointer将其强制转换如下
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: 如果确实需要使用C数组,则可能需要将
func
方法重写为类似以下内容:
- (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: 然后在Swift方面:
var arrayLength: Int = 0
let pointer: UnsafeMutablePointer<CGPoint> = yourInstance.pointArray(&arrayLength)
let swiftArray = Array(UnsafeBufferPointer(start: pointer, count: arrayLength))
free(pointer)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.