I have a problem with the String.withCString{} in combination with UnsafeMutablePointer(mutating: ...).
Given: a C-Function like this
randomSign(xml_string: UnsafeMutablePointer<Int8>!) -> Uint
Also given: a String like
str = "some xml_tag"
My working code is like this (VERSION_1)
func randomSignWrapper_1(xml_tag_str: String) -> Uint {
let result = xml_str.withCString { s in return
randomSign(UnsafeMutablePointer(mutating: s))
}
return result
}
But I want the withCString to be put into a separate function like this:
func testFunction(_ x: String) -> UnsafeMutablePointer<Int8>{
return x.withCString{s in
return UnsafeMutablePointer(mutating: s)
}
}
So that I can easily reuse it (VERSION_2)
func randomSignWrapper_2(xml_tag_str: String) -> Uint {
let result = randomSign(testFunction(xml_tag_str))
return result
}
But the problem is that VERSION_1 delivers the right return value while VERSION_2 is somehow not working properly, and telling me that the data is wrong. And I would like to know why it behaves like this? And how to solve it that I can use it the described way?