如何运行一系列功能(如果其中一个执行),则返回

时间:2018-11-28 15:08:43

标签: ios swift

我正在像我的prepareForSegue函数中初始化容器视图控制器:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    initialiseTopViewController(for: segue)
    initialiseMessagesViewController(for: segue)
    initialiseToolbarViewController(for: segue)
    initialiseTicketViewController(for: segue)
}

由于segue标识符上的guard条件,每个segue实际上只能运行这些功能之一。我该如何做,以便如果执行其中一个功能,而不会继续尝试执行其他功能?有没有比仅使用if...else if块更好的方法

4 个答案:

答案 0 :(得分:1)

一种解决方案是,如果每个initialise函数处理完序列,则返回true,否则返回false。然后:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    _ = (false
        || initialiseTopViewController(for: segue)
        || initialiseMessagesViewController(for: segue)
        || initialiseToolbarViewController(for: segue)
        || initialiseTicketViewController(for: segue)
    )
}

private func initialiseTopViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseMessagesViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseToolbarViewController(for segue: UIStoryboardSegue) -> Bool { ... }
private func initialiseTicketViewController(for segue: UIStoryboardSegue) -> Bool { ... }

另一种解决方案是将适当的测试提升到prepare(for:sender:)中,例如使用switch

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch (segue.identifier ?? "", segue.destination) {
        case (_, let dest as TopViewController): initialiseTopViewController(dest)
        case (_, let dest as MessagesViewController): initialiseMessagesViewController(dest)
        case ("toolbar", let dest as MultipurposeViewController): initialiseToolbarViewController(dest)
        case ("ticket", let dest as MultipurposeViewController): initialiseTicketViewController(dest)
        default: super.prepare(for: segue, sender: sender)
    }
}

private func initialiseTopViewController(_ controller: TopViewController) { ... }
private func initialiseMessagesViewController(_ controller: MessagesViewController) { ... }
private func initialiseToolbarViewController(_ controller: MultipurposeViewController) { ... }
private func initialiseTicketViewController(_ controller: MultipurposeViewController) { ... }

答案 1 :(得分:1)

类似于@rob mayoff的建议,让每个函数返回Bool并循环遍历包含所有函数的数组,从而使其完全由数据驱动。

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   let funs = [ initialiseTopViewController(for:), 
                initialiseMessagesViewController(for:), 
                initialiseToolbarViewController(for:), 
                initialiseTicketViewController(for:) ]

   for fun in funs {
       if fun(segue) {
           break
       }
   }
}

答案 2 :(得分:0)

我建议使用switch语句检查/广播destination的各种类型,并将该类型的值传递给适当的方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    switch segue.destination {
    case let destination as FooViewController: prepare(destination)
    case let destination as BarViewController: prepare(destination)
    case let destination as BazViewController: prepare(destination)
    default: super.prepare(for: segue, sender: sender)
    }
}

func prepare(_ destination: FooViewController) {
    destination.foo = foo
}

func prepare(_ destination: BarViewController) {
    destination.bar = bar
}

func prepare(_ destination: BazViewController) {
    destination.baz = baz
}

switch仅调用适当的方法,并将那些单独的方法保存为根本不需要执行任何guard语句。

答案 3 :(得分:0)

我想通过使函数返回bool

来添加@rob mayoff的解决方案
// make each function return boolean values so you know if it executed properly or not based on the return value
func initialiseTopViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseMessagesViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseToolbarViewController(for segue: UIStoryboardSegue) -> Bool { ... }
func initialiseTicketViewController(for segue: UIStoryboardSegue) -> Bool { ... }

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // if any function returns true, then return and don't run the remaining functions
    if initialiseTopViewController(for: segue) { return }
    if initialiseMessagesViewController(for: segue) { return }
    if initialiseToolbarViewController(for: segue) {return }
    if initialiseTicketViewController(for: segue) { return }
}

很抱歉,如果这与其他建议类似。我只是想向您展示如何使用内联if语句轻松实现目标。