Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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
Ios 如何解决可选协议方法中不能使用的参数规则?_Ios_Swift_Sonarqube - Fatal编程技术网

Ios 如何解决可选协议方法中不能使用的参数规则?

Ios 如何解决可选协议方法中不能使用的参数规则?,ios,swift,sonarqube,Ios,Swift,Sonarqube,我在协议中有一些可选方法,它们的参数没有被使用,因为它没有实现 我无法找到一种方法来满足SonarQube规则“删除未使用的参数”,而不删除该参数。有什么线索吗 public protocol InfoProtocol { func getInfo(viewController: UIViewController) func setInfo(viewController: UIViewController) } public extension InfoProtocol {

我在协议中有一些可选方法,它们的参数没有被使用,因为它没有实现

我无法找到一种方法来满足SonarQube规则“删除未使用的参数”,而不删除该参数。有什么线索吗

public protocol InfoProtocol {
    func getInfo(viewController: UIViewController)
    func setInfo(viewController: UIViewController)
}

public extension InfoProtocol {
    func getInfo(viewController: UIViewController) {
        // default implementation
    }

    func setInfo(viewController: UIViewController) {
        // default implementation
    }

} 

请尝试将
InfoProtocol
中的方法设置为
可选
,以防您不希望在任何地方都实现它们,也不希望使用
协议扩展提供任何默认实现,即

@objc protocol InfoProtocol {
    @objc optional func getInfo(viewController: UIViewController)
    @objc optional func setInfo(viewController: UIViewController)
}
用法:

class ViewController: UIViewController, InfoProtocol {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
在上面的代码中,由于
InfoProtocol
中的方法是
可选的
,因此不在
ViewController
中实现它们不会引发任何
编译时错误