Category function name collision, how do I specify which category to use?

125 Views Asked by At

I'm using AFNetworking's UIImageView+AFNetworking category to asynchronously load images in my application. I added the AppBoy SDK and it specifies SDWebImage as a dependency. Both frameworks implement a function called setImageWithURL. Is there a way to specify which framework category function I want to use when I call setImageWithURL on an UIImageView?

Sample code:

[imageView1 setImageWithURL:[NSURL URLWithString:imageSource1]];

calls SDWebImage's function and not AFNetworking's.

1

There are 1 best solutions below

0
On

You can't with Objective-C classes. If two categories implement the same selector on a class it's undefined which one gets used. This is a consequence of the way message dispatch works in ObjC.

For a pure Swift class you could chose which extension method to use by only importing the framework you actually want to use in your code. For extensions on Swift classes static dispatch is used.