iOS Swift 协议的基本用法protocol

简介

记录swift下协议的用法,与oc基本类似,基本一对一

1、定制协议

protocol textFieldSearchResultSelectViewClickDelegate: NSObjectProtocol {
    func testFieldSearchResultSelectViewClick(str: String)
}

2、声明

weak var delegate:textFieldSearchResultSelectViewClickDelegate?

3、使用 传输

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        delegate?.testFieldSearchResultSelectViewClick(str: "测试数据")
    }

4、遵守协议, 实现协议

class PunchVC: testFieldSearchResultSelectViewClickDelegate {}
searchView.delegate = self
func testFieldSearchResultSelectViewClick(str: String) {
        self.pModel.shopName = str
    }

你可能感兴趣的:(iOS Swift 协议的基本用法protocol)