码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-Swift协议中关联类型的使用

时间:2017-05-18 16:44:57      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:prot   关联   eal   alias   style   content   方法   继承   tco   

Associated Type :关联类型

有时候会遇到这样的需求,继承自某一协议的类中需要保证代理方法处理的参数类型保持一致,这就需要使用关联类型来做到这一点

协议声明:

protocol ATypeDelegate {

    associatedtype T

    func printContent( ct : T)

    func colum( ct : T)

}

协议使用:

class ATypeClass: ATypeDelegate {

    typealias T = Int

    func printContent(ct: Int) {

        print("Int",#function)

    }

    

    func colum(ct: Int) {

        print("Int",#function)

    }

}

 

ATypeClass 继承自ATypeDelegate,必须在ATypeClass类中定义 T 的具体类型才能使用

iOS-Swift协议中关联类型的使用

标签:prot   关联   eal   alias   style   content   方法   继承   tco   

原文地址:http://www.cnblogs.com/madarax/p/6873762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!