Search code examples
iosobjective-cxcodeios5ios6

Why is Xcode thinking my variable is returning an NSURL when it should be returning an NSString?


I get the following warning: "Incompatible pointer types assigning NSString from NSURL".

Here's the code in question:

cell.articleURL.text = [self.articles[row] URL];

I'm assigning a label the value of an Article object's URL property. The self.articles array is only used to hold Article objects.

In the Article class I have this:

@property (nonatomic, strong) NSString *URL;

Why is it thinking it's an NSURL?


Solution

  • The problem is that self.articles[row] will be returning an object of type id, which means that the Objective-C runtime can't figure out which method implementation applies. It's finding a URL method in another interface and using that one.

    There's more information about this problem in the article A big weakness in Objective-C's weak typing by Matt Gallagher.