Is it possible to call methods defined in a protocol extension in Swift from Objective-C?
For example:
protocol Product {
var price:Int { get }
If it is ok to remove the priceString from the protocol, and only have it in your extension, you can call the protocol extension by casting IceCream to a Product in a helper extension.
@objc protocol Product {
var price:Int { get }
}
extension Product {
var priceString:String {
return "$\(price)"
}
}
// This is the trick
// Helper extension to be able to call protocol extension from obj-c
extension IceCream : Product {
var priceString:String {
return (self as Product).priceString
}
}
@objc class IceCream: NSObject {
var price: Int {
return 2
}
}