2

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:

incompatible pointer type warning - screenshot

And from the screen shot below you can see that my address object is declared as a NSString.

property type - screenshot

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.

Perception
  • 79,279
  • 19
  • 185
  • 195
Kin
  • 171
  • 1
  • 14
  • 3
    Instead of posting a *picture* of a few lines of code, please include the actual code in your question, and be sure to show enough that we can understand what's going on. For example, what is `LocationData`? – Caleb Mar 03 '12 at 14:04
  • You posted the same image twice, can you show the code you used to declare `address`. And it is better to do that by posting your code here. – sch Mar 03 '12 at 14:04
  • What is the declaration for address? – freespace Mar 03 '12 at 14:05
  • How is this LocationData composed? It's impossible to answer a question on custom object without knowing anything about it. – Rok Jarc Mar 03 '12 at 14:09
  • what did you add inside `LocationData`, the warning clearly says, `address` is `NSData`, you need to pass `NSString` to get it right. – HelmiB Mar 03 '12 at 14:12
  • Sorry for the same image, i had updated the image. Btw, Thanks for all the reply. the warning was solved by @HelmiB reply. I will include more information next time. – Kin Mar 03 '12 at 16:10

2 Answers2

4

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 *).

ayoy
  • 3,835
  • 19
  • 20
  • 2
    The compiler doesn't just guess at which method to call, no matter what the type of the pointer. In fact, the method that'll be executed isn't determined until the call actually happens at run time -- the compiler doesn't determine it. The method that's called is determined by what the object actually is, not by the type of the pointer that's used to refer to it. – Caleb Mar 03 '12 at 14:22
  • You can catch these errors by switching on the strict selector matching warning in the build settings. – Jim Mar 03 '12 at 14:22
1

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...

HelmiB
  • 12,303
  • 5
  • 41
  • 68
  • Thanks for your reply. Your self.displayAddress.text = [NSString stringWithFormat:@"%@"[[LocationData objectAtIndex:rowDisplay] address]]; solve the problem. I used image because i wanna show out the compiler error. Next time i will include my code. Sorry. – Kin Mar 03 '12 at 16:11