Say I have a convenience initializer in Swift:
extension UIImage {
convenience init?(bundleNamed name: String) {
let bundle = NSBundle(forClass: Foo.self)
self.init(named: name, inBundle: bundle, compatibleWithTraitCollection: nil)
}
}
How might I call this in Objective-C? The following doesn't work:
[UIImage bundleNamed:@"Bar"];
[[UIImage alloc] initWithBundleNamed:@"Bar"];
Do I need an additional class extension solely for Objective-C?
Solution: following Lasse's answer below, the steps I had to do were:
In the Objective-C classes implementation file, add
#import <ModuleName-Swift.h>
then I had to delete derived data and rebuild. Then I was able to use the convenience initializer as follows:
[[UIImage alloc] initWithBundleNamed: @"Bar"];
I didn't need to declare the initializer as public
because the default level of internal
was sufficient for my module.
Yes we can use it Note: @objc and public are important
@objc public init(url: URL) {
//your code
}
Check out Using Swift with Cocoa and Objective-C (Swift 2.2) - Mix and Match. What it seems to come down to is
public
, and[YourProductModuleName]-Swift.h
into your code If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With