2019-08-03

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)
}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。