I've noticed that the AFNetworking and SDWebImage categories on UIImageView have the same method name.
我注意到UIImageView上的AFNetworking和SDWebImage类别具有相同的方法名称。
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
{
...
}
and SDWebImage
和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.
在Xcode中,当我命令单击SDWebImage方法时,它将我重定向到AFNetworking方法,反之亦然。
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.
另一个相关问题“如果两个ObjC类别覆盖相同的方法会发生什么?”与此并不完全相同,因为AFNetworking和SDWebImage都将类别添加到同一个类而不是子类。在这种情况下,只使用一次类,并且两个类别似乎存在冲突。
3 个解决方案
#1
7
Name collision is indeed something that can happen in Objective-C runtime... Apple advises the use of 'prefix' on methods names.
名称冲突确实可以在Objective-C运行时发生...... Apple建议在方法名称上使用'prefix'。
The expected behaviour : only the last category/method loaded by the runtime will be effective. Which one is it ? Bad question !
预期的行为:只有运行时加载的最后一个类别/方法才有效。哪一个 ?不好的问题!
My advice : rename !
我的建议:重命名!
#2
8
Rename doesn't seem to be the best way to resolve the problem,
重命名似乎不是解决问题的最佳方法,
just use :
只需使用:
[cell.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
}];
from SDWebImage
which doesn't exist in AFNetworking
来自SDWebImage,AFNetworking中不存在
#3
2
For anyone looking to use both SDWebImage and AFNetworking as-is, here's my caveman solution to discourage use of the colliding methods:
对于任何想要同时使用SDWebImage和AFNetworking的人来说,这是我的穴居人解决方案,以阻止使用碰撞方法:
https://gist.github.com/sibljon/5957892
https://gist.github.com/sibljon/5957892
It's obviously not future proof, and is prone to breaking if the implementation of these methods changes in AFNetworking or SDWebImage. It's far from an ideal solution. In other words, at your own risk!
它显然不是未来的证据,如果这些方法的实现在AFNetworking或SDWebImage中发生变化,则很容易破坏。它远非理想的解决方案。换句话说,风险自负!
#1
7
Name collision is indeed something that can happen in Objective-C runtime... Apple advises the use of 'prefix' on methods names.
名称冲突确实可以在Objective-C运行时发生...... Apple建议在方法名称上使用'prefix'。
The expected behaviour : only the last category/method loaded by the runtime will be effective. Which one is it ? Bad question !
预期的行为:只有运行时加载的最后一个类别/方法才有效。哪一个 ?不好的问题!
My advice : rename !
我的建议:重命名!
#2
8
Rename doesn't seem to be the best way to resolve the problem,
重命名似乎不是解决问题的最佳方法,
just use :
只需使用:
[cell.imageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
}];
from SDWebImage
which doesn't exist in AFNetworking
来自SDWebImage,AFNetworking中不存在
#3
2
For anyone looking to use both SDWebImage and AFNetworking as-is, here's my caveman solution to discourage use of the colliding methods:
对于任何想要同时使用SDWebImage和AFNetworking的人来说,这是我的穴居人解决方案,以阻止使用碰撞方法:
https://gist.github.com/sibljon/5957892
https://gist.github.com/sibljon/5957892
It's obviously not future proof, and is prone to breaking if the implementation of these methods changes in AFNetworking or SDWebImage. It's far from an ideal solution. In other words, at your own risk!
它显然不是未来的证据,如果这些方法的实现在AFNetworking或SDWebImage中发生变化,则很容易破坏。它远非理想的解决方案。换句话说,风险自负!