Self.self协议扩展中的奇怪行为

时间:2018-01-23 15:06:15

标签: swift

在此示例代码中,Self.self的行为与预期不符。根据所选参数的类型,标识符属性的值不相同。

protocol Identifiable : class {
    static var identifier: String { get }
}

extension Identifiable {
    static var identifier: String {
        return String(describing: Self.self)
    }
}

class Animal : Identifiable {}
class Tiger : Animal {}

Animal.identifier // Animal
Tiger.identifier // Tiger

func identifiableIdentifier<T: Identifiable>(of type: T.Type) -> String {
    type // Tiger.Type
    return type.identifier
}

identifiableIdentifier(of: Tiger.self) // Animal 


func animalIdentifier<T: Animal>(for type: T.Type) -> String {
    type // Tiger.Type
    return type.identifier
}

animalIdentifier(for: Tiger.self) // Tiger

有人知道为什么吗?

0 个答案:

没有答案
相关问题