6

I have a library which contains this function:

void create_pointer(Pointer **pointer);

It takes a pointer's pointer and allocates memory for it. in c, I can do it like this

Pointer *pointer;
create_pointer(&pointer);

then I have a pointer's instance.

But now I want to use this function in Swift. How?

I have no details about Pointer, I only know it's a struct, defined like this

typedef struct Pointer Pointer;
4
  • None of this appears to be Objective-C related or coming from Apple's frameworks. Swift is a layer on top of the Objective-C runtime, so third-party libraries are not going to be directly compatible. Commented Jun 6, 2014 at 5:38
  • Quite no agreed, Swift is a layer on top of the Objective-C runtime, I think it has it's own runtime, and call across language is the most important feature of modern language. Commented Jun 6, 2014 at 6:34
  • Swift has components but they all tap into the existing Obj-C runtime. I agree with your point and would love to see Swift on other platforms but... that's not happening soon. To be fair, Apple has made the Obj-C runtime open source for a while now. It's just that the frameworks that others have built are not quite as good as Apple's offerings on Mac/iOS. Commented Jun 6, 2014 at 6:38
  • For me it seems there is no choice except implements the library I used again use swift? Commented Jun 6, 2014 at 7:03

1 Answer 1

7

Let's start with a C example

typedef struct {
    NSUInteger someNumber;
} SomeStruct;

void create_some_struct(SomeStruct **someStruct) {
    *someStruct = malloc(sizeof(SomeStruct));
    (*someStruct)->someNumber = 20;
}

In C, you would use it like this:

//pointer to our struct, initially empty
SomeStruct *s = NULL;  

//calling the function
create_some_struct(&s);

In Swift:

//declaring a pointer is simple
var s: UnsafePointer<SomeStruct> = UnsafePointer<SomeStruct>.null()

//well, this seems to be almost the same thing :)
create_some_struct(&s)

println("Number: \(s.memory.someNumber)"); //prints 20

Edit:

If your pointer is an opaque type (e.g. void *), you have to use

var pointer: COpaquePointer = COpaquePointer.null()

Note that Swift is not designed to interact with C code easily. C code is mostly unsafe and Swift is designed for safety, that's why the Swift code is a bit complicated to write. Obj-C wrappers for C libraries make the task much easier.

Sign up to request clarification or add additional context in comments.

4 Comments

I test your code, Xcode give me two error, first is 'withUnsafePointer': 'UnsafePointer<$T1>' is not a subtype of '()', second is 'someStruct.memory.someNumber': 'UnsafePointer<Pointer>?' does not have a member named 'memory'
@user3709449 I removed the withUnsafePointer part. It doesn't seem to be necessary.
after remove withUnsafePointer, Xcode give me this convert the expression's type 'Void' to type 'inout UnsafePointer<Pointer>?', the second error is the same
@user3709449 That's because your struct is an opaque (unknown type). See my edit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.