I want to use a C-function in my Swift project, this function should populate a Swift String array, see please the following simple example with a Swift function:
import Foundation
func fillArray(_ cmd: inout [String]) -> Int {
cmd.append("Zero")
cmd.append("One")
cmd.append("Two")
cmd.append("Three")
cmd.append("Four")
return 5
}
func manageCommand(cmdLine: String) -> Void {
var cmd = [String]()
print(cmdLine)
var number: Int = fillArray(&cmd)
print("\(number) elements")
print(cmd)
}
…which gives the following result:
5 elements
["Zero", "One", "Two", "Three", "Four"]
I would like to get the same result by calling a C-function shown below:
#include <stdio.h>
#include <string.h>
// Not objectivec but C
int c_fillArray(char arr[][10]) {
strcpy(arr[0], "Zero");
strcpy(arr[1], "One");
strcpy(arr[2], "Two");
strcpy(arr[3], "Three");
strcpy(arr[4], "Four");
return 5;
}
and the Swift code calling this function:
import Foundation
func manageCommand(cmdLine: String) -> Void {
var cmd = [String]()
print(cmdLine)
var number: CInt = c_fillArray(cmd) // This line creates an error
print("\(number) elements")
print(cmd)
}
Xcode gives me the following error:
Cannot convert value of type '[String]' to expected argument type 'UnsafeMutablePointer<(CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar, CChar)>' (aka 'UnsafeMutablePointer<(Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8)>')
I already search the internet to find how to let it work but without success, thanks for your help

