gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:54:37 25 4
gpt4 key购买 nike

这是我的协议(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com