views:

53

answers:

2

I have a following static method in one of my utility class

+ (UIImage *) getImage:(NSURL*) fromUrl { //Warning here
    NSData *urlData = [NSData dataWithContentsOfURL:fromUrl];
    UIImage *image = [[[UIImage alloc] initWithData:urlData] autorelease];
    return image;
}

For this method I'm receiving a bellow warning message

warning: incompatible Objective-C types initializing 'struct NSURL *', expected 'struct NSString *'

I did not noticed any exception, is this something I can ignore? Or how can I fix it?

+9  A: 

The warning is correct in that there is a conflicting type and, as with all warnings, you should fix it.

More likely than not, you have two getImage: methods declared, one that takes an NSString and one that takes an NSURL as their sole argument. In Objective-C, the method namespace is flat and the recommended pattern is that there be only one declared argumentation for any given selector.

There is a more subtle issue, though. getImage: is not really as descriptive as it either could be nor as descriptive as standard practice would dictate.

A better method declaration would be:

+ (UIImage *) imageFromURL: (NSURL *) anURL;

More descriptive. Less ambiguous.

bbum
@bbum Interesting! btw SO asking me to wait for 5min to accept answer.
Prashant
+2  A: 

You're probably doing this:

UIImage * image = [MyUtilityClass getImage:@"http://example.com/image.jpg"];

You should be doing this:

UIImage * image = [MyUtilityClass getImage:[NSURL URLWithString:@"http://example.com/image.jpg"]];

(ignoring naming conventions....)

Dave DeLong
If he was doing that, it'd have thrown an exception, I would bet.
bbum