A member implementation must:
- Have the same Swift name as the member it implements.
Have a question. The exists Objective-C API bridged to Swift, may contains ambiguous because Swift has default param and trailing closure syntax. Some Objective-C code will use code style like this:
@interface UIImageView (WebCache)
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options;
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
completed:(nullable SDExternalCompletionBlock)completedBlock;
@end
Generated Swift API (via ClangImporter):
extension UIImageView {
open func sd_setImage(with url: URL?, placeholderImage placeholder: UIImage?, options: SDWebImageOptions = [])
open func sd_setImage(with url: URL?, placeholderImage placeholder: UIImage?, completed completedBlock: SDExternalCompletionBlock? = nil)
}
As the proposal says, the Swift member implementation should Have the same Swift name as the member it implements.. So actually if I wan to use this feature to remove the Objective-C implementation, I need to write:
@objc(WebImage) @implementation extension UIImageView {
open func sd_setImage(with url: URL?, placeholderImage placeholder: UIImage?, options: SDWebImageOptions = []) {
// ...impl
print("Foo")
}
open func sd_setImage(with url: URL?, placeholderImage placeholder: UIImage?, completed completedBlock: SDExternalCompletionBlock? = nil) {
// ...impl
print("Bar")
}
}
But then, if I write as the code above, because this API is ambiguous in Swift, I can not call this in pure Swift code.
public class Tester {
func test() {
UIImageView().sd_setImage(with: nil, placeholderImage: nil) // <<<<----- error: Ambiguous use of 'sd_setImage'
}
}
Possible solution
The @objc @implementation should also support NS_REFINED_FOR_SWIFT and NS_SWIFT_NAME. Which means, I can update like this to separate what Swift client see and what Objective-C client see, right ?
// For Swift client's API
extension UIImageView {
open func sd_setImage(with url: URL?, placeholderImage placeholder: UIImage?, options: SDWebImageOptions = [], completedBlock: SDExternalCompletionBlock? = nil) {
// ...impl
print("Hello")
}
}
// For Objective-C client's API
@objc(WebImage) @implementation extension UIImageView {
open func __sd_setImage(with url: URL?, placeholderImage placeholder: UIImage?, options: SDWebImageOptions = []) {
// ...impl
print("Foo")
}
open func __renamed_sd_setImage(with url: URL?, placeholderImage placeholder: UIImage?, completed completedBlock: SDExternalCompletionBlock? = nil) {
// ...impl
print("Bar")
}
}