How to pass swift String to c function in Swift 5

I want to pass swift String to c function in swift 5 .

String in swift

var key = "zxcvbnmqwertyui"
var error = "noerror"

C function declaration

extern KeyRef key(char* p0, char** p1);

C Function declaration in swift

public func key(_ p0: UnsafeMutablePointer<Int8>!, _ p1: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!) -> KeyRef

Question : How can i pass swift string (i.e key and error) to c function (i.e key ) ?

Hello!

It looks like parts of this answer may have been discussed by @eskimo and @John_McCall on another thread, but I also thought I would provide some insight here in case it helped.

On the C side of the codebase setup the C files and expose the needed functions to Swift using a bridging header. If you start a Swift project and add C or ObjC files to it Xcode will ask if you want to generate a bridging header for your project.

C_String.c

void key_function(char *p0, char **p1) {
    printf("po: %s \n", p0);
    printf("p1: %s \n", *p1);
}

As mentioned above, the bridging header is the place to expose C functions, or C header files with many functions, to Swift.

SwiftString-Bridging-Header.h

// Add C Function here or optionally add C headers here.
extern void key_function(char *p0, char **p1);

In the Swift side of the codebase I am suggesting working with an NSString to provide a NULL terminated UTF8 representation of the string that UnsafeMutablePointer can use to create CChar's from and then send over to the key_function() in C.

Main.swift

// Swift Strings
var key: NSString = "zxcvbnmqwertyui"
var error: NSString = "noerror"

// Convert the Swift strings to CChars (or Int8) that map to 
var key_p0 = UnsafeMutablePointer<CChar>(mutating: key.utf8String)
var error_p1 = UnsafeMutablePointer<CChar>(mutating: error.utf8String)

// Pass the character pointers over to the key_function() function in C.
key_function(key_p0, &error_p1)

I hope this helps!

I think you should also use withExtendedLifetime on the two strings to make sure they aren't prematurely deallocated. Those UnsafeMutablePointer will not retain the strings.

1 Like

@agnosticdev Thanks for replying , your solution worked .

1 Like

@shivam201312 no problem at all. Glad to help out!

Terms of Service

Privacy Policy

Cookie Policy