[英]How to convert objective-C float * to Swift?
I want convert objective-C我要转换 objective-C
float *bitmap = [self createRGBABitmapFromImage:image.CGImage];
int bitmapOffset = 0;
&bitmap[bitmapOffset]
float * is UnsafeMutablePointer in swift but UnsafeMutablePointer is not array. float * 是 swift 中的 UnsafeMutablePointer 但 UnsafeMutablePointer 不是数组。
So I can't use bitmapOffset.所以我不能使用 bitmapOffset。
&bitmap[bitmapOffset] this type to be UnsafePointer &bitmap[bitmapOffset] 这种类型是 UnsafePointer
So I can't use bitmapOffset所以我不能使用 bitmapOffset
Yes, you can.是的你可以。 An UnsafeMutablePointer is subscriptable, just like a pointer / array in C. UnsafeMutablePointer 是可订阅的,就像 C 中的指针/数组一样。
I'll prove it.我会证明的。 Here's something like your C code — a function that returns a float*
:这是类似于您的 C 代码的代码 — 返回float*
的 function:
float arr[] = {(float)1.1, (float)2.1, (float)3.1};
float* doSomething() {
return arr;
}
Just as you correctly say, doSomething
is seen in Swift as returning an UnsafeMutablePointer.正如您所说的那样, doSomething
在 Swift 中被视为返回一个 UnsafeMutablePointer。 Let's retrieve it — and subscript it!让我们检索它 - 并下标!
let floats = doSomething()
let oneFloat = floats[0]
let anotherFloat = floats[1]
print(oneFloat) // 1.1
print(anotherFloat) // 2.1
(However, if you are translating all the code, maybe your method should in fact return an array; I have no idea, since you didn't show it.) (但是,如果您要翻译所有代码,也许您的方法实际上应该返回一个数组;我不知道,因为您没有展示它。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.