ios - 调用协议(protocol)的 mutating 函数生成 "has no member ' functionName'"错误

标签 ios swift struct swift-protocols

这是我的协议(protocol):

protocol RequestLocationAuthorizationPresenterProtocol {
    mutating func handleLoad(for view: RequestLocationAuthorizationViewProtocol)
    func handleGivePermissionAction()
}

这是我的协议(protocol)实现:

struct RequestLocationAuthorizationPresenter: RequestLocationAuthorizationPresenterProtocol {
    private let interactor: RequestLocationAuthorizationInteractorProtocol
    private let router: RequestLocationAuthorizationRouterProtocol
    private weak var view: RequestLocationAuthorizationViewProtocol!

    init(with interactor: RequestLocationAuthorizationInteractorProtocol,
     router: RequestLocationAuthorizationRouterProtocol) {
        self.interactor = interactor
        self.router = router
    }

    mutating func handleLoad(for view: RequestLocationAuthorizationViewProtocol) {
        self.view = view
    }

    func handleGivePermissionAction() {
        self.interactor.requestAuthorization { result in
            switch result {
            case .success:
                self.router.goToWeatherView()
            case .error:
                self.view.presentAlert(with: "Error", message: "The app needs your location in order to work.")
            }
        }
    }
}

当我在我的 View 类上调用函数“handleLoad”时,它编译完美。但是,当我从模拟结构中调用它时,它会给我这个错误:“Value of type 'RequestLocationAuthorizationPresenterProtocol?'没有成员“handleLoad””

这是我的 View 类:

class RequestLocationAuthorizationView: UIViewController {
    private let presenter: RequestLocationAuthorizationPresenterProtocol

    init(with presenter: RequestLocationAuthorizationPresenterProtocol) {
        self.presenter = presenter
        super.init(nibName: "RequestLocationAuthorizationView", bundle: .main)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func presentAlert(with title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        self.present(alert, animated: true, completion: nil)
    }

    @IBAction func givePermissionButtonPressed(_ sender: Any) {
        self.presenter.handleGivePermissionAction()
    }
}

这是我的模拟结构:

class RequestLocationAuthorizationViewMock: RequestLocationAuthorizationViewProtocol {
    private let expectation: XCTestExpectation!
    private let expectedTitle: String!
    private let expectedMessage: String!
    private let presenter: RequestLocationAuthorizationPresenterProtocol!

    init(with expectation: XCTestExpectation? = nil,
         expectedTitle: String? = nil,
         expectedMessage: String? = nil,
         presenter: RequestLocationAuthorizationPresenterProtocol? = nil) {
        self.expectation = expectation
        self.expectedTitle = expectedTitle
        self.expectedMessage = expectedMessage
        self.presenter = presenter
    }

    func callPresenterHandleLoad() {
        self.presenter.handleLoad(for: self)
    }

    func callPresenterHandleGivePermissionAction() {
        self.presenter.handleGivePermissionAction()
    }

    func presentAlert(with title: String, message: String) {
        if title == self.expectedTitle && message == self.expectedMessage {
            self.expectation.fulfill()
        }
    }
}

当我将我的实现更改为类而不是结构并删除 mutating 词时,它也能完美运行。我试图寻找类似的错误,但我没有运气。我在 Xcode 10.1 上使用 Swift Compiler 4.2。

欢迎就此问题提出任何想法。

最佳答案

在更仔细地研究这个问题之后,我意识到我正在尝试将一个变异函数称为一个常量值 (let),这就是它不起作用的原因。这里的问题是编译器给我一个无意义的错误。我将我的属性从 let 更改为 var,现在可以使用了。

关于ios - 调用协议(protocol)的 mutating 函数生成 "has no member ' functionName'"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53905866/

相关文章:

iphone - 在iOS下以实用方式仅播放YouTube API中的音频

ios - 安装pod文件后改了工程名,没想到遇到如下问题

swift - 符合协议(protocol)的类作为 Swift 中的函数参数

c - 链表和内存地址

c - 如何保存返回结构体指针的函数

c - C中双向链表实现的pop方法,需要使用free吗

ios - 在 Swift 中使用 UIAlertController 的内存泄漏

ios - Alamofire json 请求 Swift 3

ios - 为什么paymentQueue(_ :shouldAddStorePayment:_:) not being called?

ios - 我们可以将 xcode 项目 (xcodeproj) 转换为 swift playground (.playground) 格式吗?