objective-c 将 NSString 转换为字符问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/589662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:11:44  来源:igfitidea点击:

casting a NSString to char problem

objective-ciphone

提问by issac

i want to casting my NSString to a constant char the code is shown below :

我想将我的 NSString 转换为常量字符,代码如下所示:

NSString *date = @"12/9/2009";
char datechar = [date UTF8String]

NSLog(@"%@",datechar);

however it return the warning assignment makes integer from pointer without a cast and cannot print the char properly,can somebody tell me what is the problem

但是它返回警告赋值从指针生成整数而没有强制转换并且无法正确打印字符,有人可以告诉我是什么问题

回答by Joel Levin

Try something more like this:

尝试更像这样的事情:

NSString* date = @"12/9/2009";
char* datechar = [date UTF8String];
NSLog(@"%s", datechar);

You need to use char* instead of char, and you have to print C strings using %s not %@ (%@ is for objective-c id types only).

您需要使用 char* 而不是 char,并且您必须使用 %s 而不是 %@ 打印 C 字符串(%@ 仅适用于objective-c id 类型)。

回答by Marc Novakowski

I think you want to use:

我想你想使用:

const char *datechar = [date UTF8String];

(note the * in there)

(注意那里的 *)

回答by Heider Sati

Your code has 2 problems:

您的代码有两个问题:

1) "char datechar..." is a single-character, which would only hold one char / byte, and wouldn't hold the whole array that you are producing from your date/string object. Therefore, your line should have a (*) in-front of the variable to store multi characters rather than just the one.

1) "char datechar ..." 是一个单字符,它只能保存一个字符/字节,并且不会保存您从日期/字符串对象生成的整个数组。因此,您的行应该在变量前面有一个 (*) 来存储多个字符,而不仅仅是一个。

2) After the above fix, you would still get a warning about (char *) vs (const char *), therefore, you would need to "cast" since they are technically the same results. Change the line of:

2) 在上述修复之后,您仍然会收到关于 (char *) 与 (const char *) 的警告,因此,您需要“强制转换”,因为它们在技术上是相同的结果。更改以下行:

char datechar = [date UTF8String];

char datechar = [date UTF8String];

into

进入

char *datechar = (char *)[date UTF8String];

char *datechar = (char *)[date UTF8String];

Notice (char *) after the = sign, tells the compiler that the expression would return a (char *) as opposed to it's default (const char *).

注意 = 符号后的 (char *) 告诉编译器该表达式将返回一个 (char *) 而不是它的默认值 (const char *)。

I know you have already marked the answer earlier however, I thought I could contribute to explain the issues and how to fix in more details.

我知道您之前已经标记了答案,但我想我可以贡献更多细节来解释问题以及如何解决。

I hope this helps.

我希望这有帮助。

Kind Regards Heider

亲切的问候海德

回答by mouviciel

I would add a * between char and datechar (and a %s instead of a %@):

我会在 char 和 datechar 之间添加一个 * (和一个 %s 而不是一个 %@):

NSString *date=@"12/9/2009"; char * datechar=[date UTF8String];
NSLog(@"%s",datechar);

回答by MaxEcho

I was suffering for a long time to convert NSStringto charto use for this function

我很痛苦了很长的时间来转换NSStringchar使用此功能

-(void)gpSendDTMF:(char) digit callID: (int)cid;

I have tried every answer of this question/many things from Google search but it did not work for me.

我已经尝试了这个问题的每一个答案/谷歌搜索中的很多东西,但它对我不起作用。

Finally I have got the solution.

最后我得到了解决方案。



Solution:

解决方案:

NSString *digit = @"5";

char dtmf;

char buf[2];

sprintf(buf, "%d", [digit integerValue]);

dtmf = buf[0];