I've noticed that the AFNetworking and SDWebImage categories on UIImageView have the same method name.
AFNetworking:
- (void)setImageWithURL:(NSURL *)url {
[self setImageWithURL:url placeholderImage:nil];
}
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
[self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
}
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
{
...
}
and SDWebImage
- (void)setImageWithURL:(NSURL *)url
{
[self setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
[self setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
{
[self setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
}
...
In Xcode when I command-click the SDWebImage method it redirects me to the AFNetworking method and vice-versa.
Which behavior can I expect where? Should I only include the header for the the appropriate category in a class I'd like to use it in? What if a same class needs to use both implementations of the category?
Another related question "What happens if two ObjC categories override the same method?" isn't quite the same as this because both AFNetworking and SDWebImage are adding Categories onto the same class not a subclass. In this case only once class is being used and 2 categories seem to be in conflict.
Having two or more methods named the same in the same class is called overloading.
Yes, we can define multiple methods in a class with the same name but with different types of parameters.
If both methods have the same parameter types, but different return types, then it is not possible.
The practice of defining two or more methods within the same class that share the same name but have different parameters is called overloading methods.
Name collision is indeed something that can happen in Objective-C runtime... Apple advises the use of 'prefix' on methods names.
The expected behaviour : only the last category/method loaded by the runtime will be effective. Which one is it ? Bad question !
My advice : rename !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With