I have a function called "childDidFinish" that receive a childCoordinator with type Coordinator, but when I try to compare the parameter received and the element Coordinator inside the Array the error is.
"Argument type 'Coordinator' expected to be an instance of a class or class-constrained type"
This is my Swift code, Thanks for your help
private(set) var childCoordinators: [Coordinator] = []
private let navigationController: UINavigationController
init(navigationController: UINavigationController){
self.navigationController = navigationController
}
func start() {
let eventListViewController: EventListViewController = .instantiate()
let eventListViewModel = EventListViewModel()
eventListViewModel.coordinator = self
eventListViewController.viewModel = eventListViewModel
navigationController.setViewControllers([eventListViewController], animated: false)
}
func startAddEvent(){
let addEventCoordinator = AddEventCoordinator(navigationController:navigationController)
childCoordinators.append(addEventCoordinator)
addEventCoordinator.start()
}
func childDidFinish(_ childCoordinator: Coordinator){
if let index = childCoordinators.firstIndex(where: { coordinator -> Bool in
return childCoordinator === coordinator//There is the problem
}){
childCoordinators.remove(at: index)
}
}
I think your Coordinator
is a protocol
. But in order to be comparable with ===
it must be a class-only protocol, which means it should inherit from AnyObject
(in recent version of Swift; in earlier Swift versions it had to inherit from class
)
So all you need to do is add : AnyObject
to a definition of your protocol:
protocol Coordinator: AnyObject {
// ...
}
If this is not the case, then like @matt we need to see your Coordinator
.
您也可以制作Coordinator
Equatable
,无论它是什么(协议、类或结构),只需使用==
而不是===
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.