非通用 Swift class 可以实现 UIPickerViewDataSource 但通用版本不能

Non-generic Swift class can implement UIPickerViewDataSource but Generic version can't

Swift1.2/Xcode6.3.

为什么这是有效的:

class RangeDelegateNongeneric: NSObject, UIPickerViewDataSource {
    var values = [Int]()

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return values.count
    }
}

但这不是:

class RangeDelegateGeneric<T>: NSObject, UIPickerViewDataSource {
    var values = [T]()

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return values.count
    }
}

错误:Type RangeDelegateGeneric<T> does not conform to protocol UIPickerViewDataSource

更奇怪的是,Fix-it 消息:Candidate is not @objc, but protocol requires it@objc 添加到每个函数的开头,但这并没有 Fix-it,而且 Fix-it 工具很高兴重复添加 @objc!

这在 Swift 2.0 中已修复,在 Xcode 7(撰写本文时为 beta 5)中经过专门测试。

99% 确定这个博客片段解释了它,尽管对编译器书呆子更友好的显式规范声明非常难以找到:

https://developer.apple.com/swift/blog/?id=29

[强调我的]

Swift-er SDKs: Swift 2 works even better with the Apple SDKs, thanks in part to two new features in Objective-C: nullability annotations and generics. The SDKs have been updated to annotate API that cannot return nil so you don’t need to use optionals as often. And with a true generics system employed by the SDKs you can more often preserve detailed type information in your Swift 2 code.