ios - 在 guard 语句 swift 中呈现一个 ViewController

标签 ios swift guard presentviewcontroller

我正在尝试呈现 viewcontroller,以防状态(Int?)为零,如下所示:

    guard let statusCode = status else {
        DispatchQueue.main.async() {
            let initViewController = self.storyboard!.instantiateViewController(withIdentifier: "SignInViewController")
            self.present(initViewController, animated: false, completion: nil)
        }
            return
    }

我想知道这是否是最佳实践,因为在呈现 View Controller 后返回没有多大意义,但在 guard 语句中它是必需的 guard声明不能落空。

最佳答案

回答关于guard 语句和return 关键字的问题

func yourFunc() {
    guard let statusCode = status else {
      return DispatchQueue.main.async() { [weak self] _ in
        let initViewController = self?.storyboard!.instantiateViewController(withIdentifier: "SignInViewController")
        self?.present(initViewController!, animated: false, completion: nil)
    }
  }
}

关于ios - 在 guard 语句 swift 中呈现一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50266179/

相关文章:

swift - 是否可以使用 "try?"在保护语句中检索异常?

json - 为什么守卫不保护我免受 nil

ios - Firebase 分析磁盘写入失败

ios - 使用 UISearchController 按对象属性搜索?

json - SWIFT 3 如何从本地主机 Web 服务连接 Json

initialization - Swift 中 expression.init() 有什么用

rspec - 无法获得与 Growl-rspec gem 一起使用的 Growl 通知

iphone - 如何呈现 tabview Controller ?

ios - 页面 Controller 宽度 (iPhone)

IOS : Multiple storyboards (one for the login/signup, 一个用于主应用程序)