user3544504
user3544504

Reputation: 19

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.

Upvotes: 0

Views: 1683

Answers (2)

Ossir
Ossir

Reputation: 3145

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);
}

Upvotes: 1

Alexander
Alexander

Reputation: 8147

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

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

NSString Class Reference

Upvotes: 0

Related Questions