Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMutablePointer<CString> to String in swift-language

I have a library which has a function like this:

int get_user_name(const char **buffer);

in swift, should call like this:

var name:CMutablePointer<CString> = nil
get_user_name(name)

I want make use this function more comfortable so I wrapped this up:

func get_username() -> String {
    var name:CMutablePointer<CString> = nil
    get_user_name(name)

    // how to convert name to String
}

I question is how to convert name to String

like image 414
galilio Avatar asked Nov 26 '25 20:11

galilio


1 Answers

It goes something like:

 var stringValue :CString = ""

 name.withUnsafePointer {p in
      stringValue = p.memory
 }

 return NSString(CString: stringValue)
like image 58
iluvcapra Avatar answered Nov 28 '25 15:11

iluvcapra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!