Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何使类符合Swift中的协议?_Objective C_Swift - Fatal编程技术网

Objective c 如何使类符合Swift中的协议?

Objective c 如何使类符合Swift中的协议?,objective-c,swift,Objective C,Swift,在目标C中: @interface CustomDataSource : NSObject <UITableViewDataSource> @end 但是,将出现一条错误消息: 类型“CellDataSource”不符合协议“NSObjectProtocol” 类型“CellDataSource”不符合协议“UITableViewDataSource” 正确的方法应该是什么 类型“CellDataSource”不符合协议“NSObjectProtocol” 您必须使类继承自NSO

在目标C中:

@interface CustomDataSource : NSObject <UITableViewDataSource>

@end
但是,将出现一条错误消息:

  • 类型“CellDataSource”不符合协议“NSObjectProtocol”
  • 类型“CellDataSource”不符合协议“UITableViewDataSource”
  • 正确的方法应该是什么

    类型“CellDataSource”不符合协议“NSObjectProtocol”

    您必须使类继承自
    NSObject
    ,以符合
    NSObjectProtocol
    。香草雨燕类则不然。但是
    UIKit
    的许多部分都需要
    NSObject
    s

    class CustomDataSource : NSObject, UITableViewDataSource {
    
    }
    
    但这是:

    类型“CellDataSource”不符合协议“UITableViewDataSource”

    这是意料之中的事。在类实现协议的所有必需方法之前,您将收到错误


    所以获取编码:)

    类必须先从父类继承,然后才能符合协议。主要有两种方法

    一种方法是让类继承自
    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
        }
    }
    
    然而,这有时会使代码变得混乱,因为您可能需要遵守许多协议,并且每个协议可能有几个委托函数。在这种情况下,您可以使用
    扩展
    将符合协议的代码从主类中分离出来,并且不需要在扩展中添加
    覆盖
    关键字。因此,上述代码的等效项为

    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
        }
    }
    

    Xcode 9,帮助实现Swift数据源和委托的所有强制方法

    以下是
    UITableViewDataSource
    的示例:

    显示实现强制方法的警告/提示:

    单击“修复”按钮,它将在代码中添加所有必需的方法:


    错误消息中的类名似乎与提供的代码不匹配?默认情况下,Swift类不会从NSObject继承。除非另有说明,否则它们是自己的基类;你救了我一天,因为我一直在努力让我的Swift类符合UICollectionViewDataSource协议。在我的类中添加NSObject继承解决了这个问题!我是唯一一个认为编译警告足够的人吗?@Magoo你的意思肯定是不够的“不符合协议”对我来说并不意味着“继承自NSObject”。@RoyFalk我的意思是编译警告对错误已经足够了。。。您可能不需要在所有情况下都实现整个协议,并且可能希望在实现之前先进行构建。。。这没什么大不了的,但感觉有点不必要。
    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
        }
    }