将UITextField(field.text)NSString *转换为char *?

时间:2013-01-14 13:57:42

标签: iphone ios objective-c xcode nsstring

我想将NSString* str转换为char* ch,以便将其用作此函数中的参数: str来自UITextField's文字。

void CUserSession::Login (char *pUsername, char *pPassword)

请帮忙。

使用

ch = [str UTF8String];

//我收到了错误。

Cannot initialize a parameter of type 'char *' with an rvalue of type 'const char *'

然后使用

ch = (const uint8_t *)[str UTF8String];

//我收到错误

Cannot initialize a parameter of type 'char *' with an rvalue of type 'const uint8_t *' (aka 'const unsigned char *')

2 个答案:

答案 0 :(得分:3)

替换它

ch = [str UTF8String];

const char *ch = [str UTF8String];

或尝试:

ch = (char *)[str UTF8String];

答案 1 :(得分:1)

为什么不尝试明显的解决方案?正确声明指针:

const char *ch = [str UTF8String];