Swift 泛型和Protocol 用法

简单的Protocol 范性用法,还可以针对复杂的扩展。


import Foundation
import UIKit

protocol RuleProtocol {
    associatedtype T: CustomStringConvertible
    var title: String {get set}
    var items: [T] {get}
    
    func itemSelect(_ index: Int, action: (_ item: T) -> ())
    func cellFor(_ indexPath: IndexPath) -> UITableViewCell
}

extension RuleProtocol {
    
    func itemSelect(_ index: Int) -> T {
        let item = items[index]
        return item
    }
}

实战1:

struct MySource: RuleProtocol {
    
    typealias T = String
    
    weak private(set) var tableView: UITableView?
    
    var title: String = "123"
    
    var items: [MySource.T] = ["4", "5", "6"]
    
    init(_ tableView: UITableView?) {
        self.tableView = tableView
    }
    
    init(_ title: String, tableView: UITableView?) {
        self.init(tableView)
        self.title = title
    }
    
    func cellFor(_ indexPath: IndexPath) -> UITableViewCell {
         guard let tableView = tableView else {
            return UITableViewCell()
        }
        let reuseIdentifier = "FreeGiftsTableViewCell"
        var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: reuseIdentifier)
        }
        guard let cellOne = cell else {
            return UITableViewCell()
        }
        cellOne.textLabel?.text = items[indexPath.row]
        return cellOne
    }
    
    func itemSelect(_ index: Int, action: (String) -> ()) {
        let item = items[index]
        action(item)
    }
}

用例:


import UIKit

class TableViewController: UITableViewController {
    lazy var mySource: MySource = {
        return MySource(self.tableView)
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
        title = mySource.title
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return mySource.items.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return mySource.cellFor(indexPath)
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        mySource.itemSelect(indexPath.row) { (value) in
            print(value.description)
        }
    }
}

你可能感兴趣的:(Swift 泛型和Protocol 用法)