给定以下数组:

let ignoredViewControllerTypes:[UIViewController.Type] = [
    ViewControllerB.self,
    ViewControllerC.self
]

let allViewControllers = [
    viewControllerAInstance,
    viewControllerBInstance,
    viewControllerCInstance,
    viewControllerDInstance
]

过滤allViewControllers以便排除ignoredViewControllerTypes中那些类型的语法是什么?
我试过,但没用:
let filteredControllers = allViewControllers.filter{ !ignoredViewControllerTypes.contains($0.self) }

那我错过了什么?

最佳答案

这应该有效:

let filteredControllers = allViewControllers.filter { viewController in
    !ignoredViewControllerTypes.contains(where: { type(of: viewController) == $0 })
}

关于swift - 如何检查对象是否为类型数组之一?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49181374/

10-10 20:11