简体   繁体   中英

How do I convert NSString to const void

I am currently having issues converting NSString to a const void *

Here is my code:

DoSomethingFunction:(const void *)parameter

 [class DoSomeThingFunction:(const void *)passswordField.text]

Password field is a UITextfield . The value becomes null when I try and cast it.

I want passwordField.text to be a const void * so it can be used in the function.

It depends on function implementation. It can be like this:

NSString *string = @"text";
const void *parameter = CFBridgingRetain(string);
DoSomethingFunction(parameter);

If function has similar parameter handling

void DoSomethingFunction(const void *parameter) {
    NSString *string = CFBridgingRelease(parameter);
    NSLog(@"%@", string);
}

Try casting the result of one of the NSString's methods that return a C string:

– cStringUsingEncoding:
– getCString:maxLength:encoding:
– UTF8String

NSString Class Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM