Declare as NSString but Xcode treat as NSdata & received incompatible passing
-
04-12-2019 - |
Pergunta
I'm trying to pass an NSString address object to a UILabel text, but I received a warning from XCode compiler about incompatible pointer, as shown in the screen shot below:
And from the screen shot below you can see that my address object is declared as a NSString.
I had try displaying my NSString with:
NSLog(@"%@",[[LocationData objectAtIndex:rowDisplay] address]);
and it works without any incompatible pointer error. Can anyone please help? I have done some researching but I still can't find any solution.
My address is an object which gets stored into an NSArray. The address format is the always the same, for example "542 W 112 St New York NY 10025".
Thanks.
Solução
there is many follow up questions with your question. However, you said, you can show with :
NSLog(@"%@",[[LocationData objectAtIndex:rowDisplay] address]);
so why not use this into UILabel
:
self.displayAddress.text = [NSString stringWithFormat:@"%@"[[LocationData objectAtIndex:rowDisplay] address]];
PS: please don't use image for 1 line of code. i have to retype your code here...
Outras dicas
This might be due to the fact that there already is a method/property called address
in some of SDK classes, and it happens to return NSData *
.
NSArray
's objectAtIndex:
returns an object of type id
, and the compiler doesn't know that it's your custom class that has address
defined to return NSString *
. When it tries to match address
selector, it takes the one from SDK, and not yours.
You can however cast the returned object to your class and have your method address
called:
[(YourClass *)[LocationData objectAtIndex:rowDisplay] address];
You don't see the warning when outputting address
to NSLog
since %@
format accepts both NSString *
and NSData *
(for classes other than NSString
it actually outputs the result of description
method, which returns NSString *
).