Based on whether the type is explicitly declared or inferred the below code gives different outputs.
- Why does it behave this way. Why can't I implement myFloat in my class if I declare the type?
- What do I need to do if i want to declare the type explicitly but also override the implementation in the extension so that the call to testClass1.myFloat() returns 2.7?
This with Xcode 8.1, Swift 2.3
import UIKit
protocol TestSource {
func myFloat() -> Float
}
extension TestSource {
func myFloat() -> Float {
return 2.5
}
}
protocol TestProtocol: TestSource {
}
class DevClass: TestProtocol {
}
class TestClass: DevClass {
func myFloat() -> Float {
return 2.7
}
}
let testClass1: TestProtocol = TestClass()
testClass1.myFloat() // Returns 2.5
let testClass2 = TestClass()
testClass2.myFloat() // Returns 2.7