美文网首页
2019-08-03

2019-08-03

作者: hhcszgd | 来源:发表于2019-08-03 11:58 被阅读0次

Something about protocol of swift


protocol GrandProtocol {
    associatedtype ExpectType : Codable
    func printt(codable : ExpectType) -> ExpectType
}

class TypeA : GrandProtocol{
    typealias TypeAExpectType = String//just confirm codable
    func printt(codable: TypeAExpectType) -> TypeAExpectType {
        return "value to be return"
    }
}


class TypeZ : Decodable , Encodable {}
protocol SubProtocolCertainExpectType : GrandProtocol where ExpectType == TypeZ {}

class TypeC  : SubProtocolCertainExpectType{
    typealias TypeCExpectType = TypeZ //must be TypeZ
    func printt(codable : TypeCExpectType) -> TypeCExpectType{
        return TypeZ()
    }
}


protocol SubProtocolWhichExpectTypeIskindOfTypeZWhichConfirmCodable : GrandProtocol where ExpectType : TypeZ {}
class SubTypeZ: TypeZ {
    
}
class TypeD  : SubProtocolWhichExpectTypeIskindOfTypeZWhichConfirmCodable{
    typealias TypeDExpectType = SubTypeZ//should be TypeZWhichConfirmCodable or sub class of TypeZWhichConfirmCodable
    func printt(codable : TypeDExpectType) -> TypeDExpectType{
        return SubTypeZ()
    }
}



protocol RestrainPropertyType {
    associatedtype ExceptType : Codable
}
class SomeType: RestrainPropertyType {
    typealias ExceptType = String
    var property : ExceptType?
}
func usage()  {
    let resultOf =  SomeType().property?.count
    print(resultOf)
}



相关文章

网友评论

      本文标题:2019-08-03

      本文链接:https://www.haomeiwen.com/subject/xscjdctx.html