我一直在玩协议扩展,我有一个问题。也许我想达到的目标是无法完成的。我有这样的游乐场:Swift 2:UITableViewDataSource协议扩展
//: Playground - noun: a place where people can play
import UIKit
protocol ArrayContainer {
typealias T
var array: [T] { get }
}
class MyViewController: UIViewController, ArrayContainer, UITableViewDataSource {
typealias T = String
var array = ["I am", "an Array"]
}
extension UITableViewDataSource where Self: ArrayContainer {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Whatever
return UITableViewCell()
}
}
这是我有什么,我想:
- 我有一个协议
ArrayContainer
,只是有一个typealias以及包含该typealias类型的对象数组 - 我有一个
UITableViewDataSource
的协议扩展当类符合ArrayController
协议时使用。这只是返回数组的行数作为行数。cellForRowAtIndexPath
方法没有很好地实现,但它不是问题。 - 我有一个叫做
MyViewController
的UIViewController
子类,它实现了两种协议。
的问题是,编译器会抱怨,因为MyViewController不UITableViewDataSource
符合,但是,据我所知,这应该由UITableViewDataSource延伸覆盖。我在这里错过了什么吗?或者也许Objective-C协议不能被扩展?
问题可能与http://stackoverflow.com/questions/32542362/swift-2-0-uitextfielddelegate-protocol-extension-not-working – ABakerSmith
我敢肯定,这个问题是因为协议无法通过Objective-C访问扩展,并且“UITableViewDataSource”实现需要是ObjC可访问的。 – Logan
是的,我认为这是问题,但它在任何地方记录? – manueGE