Swift 클래스는 타입 다운 캐스팅이 완료되면 원래 메소드를 호출 할 수 없습니다.타입 다운 캐스팅은 Swift에서 원래의 메소드를 찾을 수 없습니다.
AFAIK, 클래스 포인터로 C/C++에서 동일한 작업을 수행 할 수 있습니다.
하자 코드 회담 :
protocol MyProtocol {
func log()
}
class MyClass: MyProtocol {
}
class MyGenericClass<T>{
init(_ t: T) {}
}
extension MyGenericClass: MyProtocol {
func log() {
print("MyGenericClass MyProtocol")
}
}
extension MyGenericClass where T==Int {
func log() {
print("MyGenericClass<Int>")
}
}
extension MyProtocol {
func log() {
print("MyProtocol")
}
}
extension MyProtocol where Self==TestSwift.MyGenericClass<Int> {
func log() {
print("MyProtocol where MyGenericClass<Int>")
}
}
extension MyProtocol where Self==MyClass {
func log() {
print("MyProtocol where MyClass")
}
}
func logByProtocol(_ p: MyProtocol) {
p.log()
print("Type of p is: \(type(of: p))")
}
let myGenericClassNumber = MyGenericClass(1)
let myGenericClassString = MyGenericClass("1")
let myClass = MyClass()
myGenericClassNumber.log()//expect "MyGenericClass<Int>"
myGenericClassString.log()//expect "MyGenericClass MyProtocol"
myClass.log()//expect "MyProtocol where MyClass"
logByProtocol(myGenericClassNumber)//expect "MyGenericClass<Int>", BUT "MyGenericClass MyProtocol"
logByProtocol(myGenericClassString)//expect "MyGenericClass MyProtocol"
logByProtocol(myClass)//expect "MyProtocol where MyClass"
내가 logByProtocol(myGenericClassNumber)
이 "MyGenericClass<Int>"
를 인쇄 할 것으로 기대하지만 "MyGenericClass MyProtocol"을 인쇄합니다.
MyProtocol
에 다운 캐스트가 수행되는 동안 MyGenericClass<Int>
은 원래 log()
메서드를 찾을 수없는 것 같습니다.
"down"
클래스를 만들려면이 문제를 해결하는 방법은 원래 메서드를 호출 할 수 있습니까? Swift dynamic dispatch이 제네릭 제약 조건을 무시하기 때문에 이런 일이
해결되었습니다. 단순히 p가 MyGenericClass 인 경우 {(! MyGenericClass로 ) .log()}'를 추가하십시오. –
문자열 generic, double generic, float generic 등 많은 generics를 작성해야합니다. 해결책이 아니라 해킹입니다! – D4ttatraya