同一类上的两个类别具有相同的方法名称 [英] Two Categories on the same Class with the same method name

查看:181
本文介绍了同一类上的两个类别具有相同的方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到UIImageView上的AFNetworking和SDWebImage类别具有相同的方法名称。

I've noticed that the AFNetworking and SDWebImage categories on UIImageView have the same method name.

AFNetworking:

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
{
...
}

和SDWebImage

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];
}

...

在Xcode中,当我执行命令时单击SDWebImage方法,它将我重定向到AFNetworking方法,反之亦然。

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?

另一个相关问题 与AFNetworking和SDWebImage都不太一样将类别添加到同一类而不是子类上。在这种情况下,只有一次使用了类,并且似乎出现了两个类别冲突。

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.

推荐答案

名称冲突确实可以解决发生在Objective-C运行时...

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 !

我的建议:重命名!

这篇关于同一类上的两个类别具有相同的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆