开发者

return from incompatible pointer types

开发者 https://www.devze.com 2023-01-12 20:29 出处:网络
The below code generates the incompatible pointer type error: char *PLURAL(int objects, NSString *singluar, NSString *pluralised) {

The below code generates the incompatible pointer type error:

char *PLURAL(int objects, NSString *singluar, NSString *pluralised) {
return objects ==1 ? singluar:pluralised;}

I am new 开发者_Python百科to objective-C and programming in general so can some one help me with this error?


An NSString * is not the same as a char * (or "C-string" in Objective C terminology). You can't convert a pointer from one to the other implicitly like that. You'll have to use a method like cStringUsingEncoding. Also, NSString is immutable, so you'll have to return a const char *.

Alternatively, you could simply return the NSString * instead of char *.


Change the return value to NSString* and you should be fine. You are specifying a return value of char* but actually returning NSString*.


Change it to:

NSString *PLURAL(int objects, NSString *singluar, NSString *pluralised) {
    return objects ==1 ? singluar:pluralised;
}

char * is not NSString !

0

精彩评论

暂无评论...
验证码 换一张
取 消