Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Categories on the same Class with the same method name

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.

like image 295
PaulWoodIII Avatar asked Feb 06 '13 03:02

PaulWoodIII


People also ask

What is it called when two methods have the same name?

Having two or more methods named the same in the same class is called overloading.

Can a class have two methods with same name?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.

Can two methods have the same name but different return type?

If both methods have the same parameter types, but different return types, then it is not possible.

What is it called when two methods have the same name but different parameters?

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.


1 Answers

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 !

like image 182
Vinzzz Avatar answered Sep 28 '22 09:09

Vinzzz