Is this unnecessary warning?
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] i开发者_如何学CnitWithData: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?
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.
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....)
精彩评论