协议方法如何在 class 中设置变量?
How does a protocol method set a variable in a class?
我正在阅读一本 Swift OOP 书籍,其中一章是关于协议的。我了解协议类似于具有 class 将采用和实施的属性和方法的合同。在完成 UITableView
教程后,我注意到 UITableViewDataSource
协议有一个 numberOfSections()
方法,该方法 returns 是一个整数。此方法还在 UITableView
实例中设置 numberOfSections
变量。我不清楚这是怎么发生的,因为我假设 UITableView
class 与 UITableViewDataSource
协议是分开的,因此不会设置它的任何属性。这是示例代码:
import UIKit
class ItemListDataProvider: NSObject, UITableViewDataSource, UITableViewDelegate
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
}
这是证明 UITableView
属性 是由 UITableViewDataSource
协议的方法设置的测试:
import XCTest
@testable import PassionProject
class ItemListDataProviderTests: XCTestCase
{
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func tests_NumberOfSectionsIsTwo(){
let sut = ItemListDataProvider()
let tableView = UITableView()
tableView.dataSource = sut
tableView.delegate = sut
let numberOfSections = tableView.numberOfSections
XCTAssertEqual(numberOfSections, 2)
}
}
我知道我们对 Apple 的 API 视而不见,但在尝试学习 Swift OOP 时,我想了解幕后发生的事情。
提前感谢您的帮助。
我不认为它在设置值。它被定义为 getter 属性。所以,这将是它最接近的定义。
open var numberOfSections: Int {
get {
return datasource.numberOfSections
}
}
Consider we are in UITableView Class and when you set the dataSource and delegate of TableView in your any controller class.For example
tableView.dataSource = yourControllerClassObject;
tableView.delgate = yourControllerClassObject;
Above code shows that you are passing your controller object to tableView Class and tableView Class holds your controller class object in it's protocol variable of "delegate and dataSource".
When you implement dataSource protocol method in your Controller Class,you are able to call those method from tableView class like below:
class tableView{
self.dataSource.numberOfSections(in tableView: self)
}
above numberOfSctions method returns an integer value of sections that we can store in any variable of class tableView like below.
class tableView{
var numberOfSections = self.dataSource.numberOfSections(in tableView: self)
}
我正在阅读一本 Swift OOP 书籍,其中一章是关于协议的。我了解协议类似于具有 class 将采用和实施的属性和方法的合同。在完成 UITableView
教程后,我注意到 UITableViewDataSource
协议有一个 numberOfSections()
方法,该方法 returns 是一个整数。此方法还在 UITableView
实例中设置 numberOfSections
变量。我不清楚这是怎么发生的,因为我假设 UITableView
class 与 UITableViewDataSource
协议是分开的,因此不会设置它的任何属性。这是示例代码:
import UIKit
class ItemListDataProvider: NSObject, UITableViewDataSource, UITableViewDelegate
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return UITableViewCell()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
}
这是证明 UITableView
属性 是由 UITableViewDataSource
协议的方法设置的测试:
import XCTest
@testable import PassionProject
class ItemListDataProviderTests: XCTestCase
{
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func tests_NumberOfSectionsIsTwo(){
let sut = ItemListDataProvider()
let tableView = UITableView()
tableView.dataSource = sut
tableView.delegate = sut
let numberOfSections = tableView.numberOfSections
XCTAssertEqual(numberOfSections, 2)
}
}
我知道我们对 Apple 的 API 视而不见,但在尝试学习 Swift OOP 时,我想了解幕后发生的事情。
提前感谢您的帮助。
我不认为它在设置值。它被定义为 getter 属性。所以,这将是它最接近的定义。
open var numberOfSections: Int {
get {
return datasource.numberOfSections
}
}
Consider we are in UITableView Class and when you set the dataSource and delegate of TableView in your any controller class.For example
tableView.dataSource = yourControllerClassObject;
tableView.delgate = yourControllerClassObject;
Above code shows that you are passing your controller object to tableView Class and tableView Class holds your controller class object in it's protocol variable of "delegate and dataSource".
When you implement dataSource protocol method in your Controller Class,you are able to call those method from tableView class like below:
class tableView{
self.dataSource.numberOfSections(in tableView: self)
}
above numberOfSctions method returns an integer value of sections that we can store in any variable of class tableView like below.
class tableView{
var numberOfSections = self.dataSource.numberOfSections(in tableView: self)
}