objective-c 如何使类符合 Swift 中的协议?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24991018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a class conform to a protocol in Swift?
提问by YuXuan Fu
in Objective-C:
在 Objective-C 中:
@interface CustomDataSource : NSObject <UITableViewDataSource>
@end
in Swift:
在斯威夫特:
class CustomDataSource : UITableViewDataSource {
}
However, an error message will appear:
但是,会出现错误消息:
- Type 'CellDatasDataSource' does not conform to protocol 'NSObjectProtocol'
- Type 'CellDatasDataSource' does not conform to protocol 'UITableViewDataSource'
- 类型“CellDatasDataSource”不符合协议“NSObjectProtocol”
- 类型 'CellDatasDataSource' 不符合协议 'UITableViewDataSource'
What should be the correct way ?
正确的方法应该是什么?
回答by Alex Wayne
Type 'CellDatasDataSource' does not conform to protocol 'NSObjectProtocol'
类型“CellDatasDataSource”不符合协议“NSObjectProtocol”
You have to make your class inherit from NSObjectto conform to the NSObjectProtocol. Vanilla Swift classes do not. But many parts of UIKitexpect NSObjects.
你必须让你的类继承NSObject以符合NSObjectProtocol. Vanilla Swift 类没有。但是UIKitexpect NSObjects 的很多部分。
class CustomDataSource : NSObject, UITableViewDataSource {
}
But this:
但是这个:
Type 'CellDatasDataSource' does not conform to protocol 'UITableViewDataSource'
类型 'CellDatasDataSource' 不符合协议 'UITableViewDataSource'
Is expected. You will get the error until your class implements all required methods of the protocol.
是期待。在您的类实现协议的所有必需方法之前,您将收到错误消息。
So get coding :)
所以得到编码:)
回答by Fangming
A class has to inherit from a parent class before conform to protocol. There are mainly two ways of doing it.
在符合协议之前,类必须从父类继承。主要有两种实现方式。
One way is to have your class inherit from NSObjectand conform to the UITableViewDataSourcetogether. Now if you want to modify the functions in the protocol, you need to add keyword overridebefore the function call, like this
一种方法是让您的类继承NSObject并符合UITableViewDataSource在一起。现在如果要修改协议中的函数,需要override在函数调用前加上关键字,像这样
class CustomDataSource : NSObject, UITableViewDataSource {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
return cell
}
}
However this sometimes gets your code messy because you may have many protocols to conform to and each protocol may have several delegate functions. In this situation, you can separate the protocol conforming code out from the main class by using extension, and you do not need to add overridekeyword in the extension. So the equivalent of the code above will be
然而,这有时会使您的代码变得混乱,因为您可能有许多协议要遵守,并且每个协议可能有几个委托函数。在这种情况下,您可以使用 将符合协议的代码从主类中分离出来extension,而无需override在扩展中添加关键字。所以上面代码的等价物将是
class CustomDataSource : NSObject{
// Configure the object...
}
extension CustomDataSource: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
// Configure the cell...
return cell
}
}
回答by Krunal
Xcode 9, helps to implement all mandatory methods of Swift Datasource & Delegates.
Xcode 9,有助于实现 Swift Datasource & Delegates 的所有强制方法。
Here is example of UITableViewDataSource:
这是示例UITableViewDataSource:
Shows warning/hint to implement mandatory methods:
显示警告/提示以实现强制方法:
Click on 'Fix' button, it will add all mandatory methods in code:
单击“修复”按钮,它将在代码中添加所有必需的方法:


