Home > Blockchain >  How to convert objective-C float * to Swift?
How to convert objective-C float * to Swift?

Time:02-24

I want convert objective-C

float *bitmap = [self createRGBABitmapFromImage:image.CGImage];
int bitmapOffset = 0;
&bitmap[bitmapOffset]

float * is UnsafeMutablePointer in swift but UnsafeMutablePointer is not array.

So I can't use bitmapOffset.

&bitmap[bitmapOffset] this type to be UnsafePointer

CodePudding user response:

So I can't use bitmapOffset

Yes, you can. An UnsafeMutablePointer is subscriptable, just like a pointer / array in C.

I'll prove it. Here's something like your C code — a function that returns a float*:

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

  • Related