Reputation: 29316
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?
Upvotes: 3
Views: 136
Reputation: 1544
When you create an NSMutableArray
at that time compiler don't know that for what type of object you will store. It is decided dynamically. So when you are trying to get URL
from
[self.articles[row] URL];
compiler treats it as id type. So you have to use type cast cell.articleURL.text = [(Article *) self.articles[row] URL];
Upvotes: 0
Reputation: 46533
cell.articleURL.text = [self.articles[row] URL];
As you said self.articles[row]
returns id
, so you need to enforce its type to be Article
.
cell.articleURL.text = [(Article *)self.articles[row] URL];
you have a method called URL that is getting called by the object returned by self.articles[row]
and its return type is NSURL
As I can not find any class having a method called URL
, then this must be there in any of the frameworks or classes / library you are using and it is getting called.
Upvotes: 0
Reputation: 9042
Adding to @Jim answer. You will need to cast the result of [self.articles[row]]
to the correct class where @property (nonatomic, strong) NSString *URL;
is declared. So the Objective-C runtime can figure out which method implementation can be applied.
i.e.
[(Article *)[self.articles[row]] URL]
Or store this in a variable of type Article
, then call URL on that object.
Upvotes: 0
Reputation: 16296
Your code doesn't know the return type of your array and so guesses which method from which class you might be calling. You need to add a cast like so, which lets the compiler know exactly which class to expect:
cell.articleURL.text = [(Article *) self.articles[row] URL];
Upvotes: 0
Reputation: 318774
Refactor your code to avoid the confusion:
Article *article = self.articles[0];
cell.articleURL.text = article.URL;
As Jim stated, the compiler is finding the wrong URL
method for the id
object. By splitting up the code, you avoid this issue.
Upvotes: 0
Reputation: 73936
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.
Upvotes: 5