Swift 协议protocol

// associatedtype 关键字 用来声明一个类型的占位符作为协议定义的一部分

protocol LXFViewModelType {

    associatedtype Input

    associatedtype Output

    

    func transform(input: Input) -> Output

}

/*

 定义协议时候,协议后面最好跟上class

 delegate的属性最好用weak,用于防止循环引用

 */

protocol BuyTicketDelegate : class{

    func buyTicket()

}

 

class Person22{

    weak var delegate:BuyTicketDelegate?

    

    func gotoBeiJing() {

        delegate?.buyTicket()

    }

}

 

 

/*

 定义可选类型的协议

 optional属于OC特性,如果协议中油可选的方法,那么必须在protocol前面加上@objc,也需要在optional前面加上@objc

 */

@objc protocol TestProtocol{

    @objc optional func test()

}

 

class Dog:TestProtocol{

    

}

posted on 2019-07-09 19:57  二狗你变了  阅读(283)  评论(0编辑  收藏  举报

导航