I want to pass to a method a void* from a NSString get in a NSTextField so I did like this :
(It is to add a new password to the keychain access on OS X)
NSString *password = [passwordTextField stringValue];
UInt32 passwordlength = (UInt32) [password length];
void *mypassword = malloc(passwordlength);
mypassword = &password;
StorePasswordKeychain(mypassword, passwordlength);
The problem is that if passwordTextField returns "hello" for example, i get back in the keychain "xhell". the x is random..
I think that it is a basic problem but i can't figure how to solve it ..
You should not assume how NSString
object store it internal representation. In your case, first byte probably occupied with string length or other NSString
internal data. Use following code instead:
NSString *password = [passwordTextField stringValue];
void *mypassword = (void *)[password UTF8String];
StorePasswordKeychain(mypassword, strlen(mypassword));
If you need different encoding, use cStringUsingEncoding:
method instead of UTF8String
.