为什么不能在 AppDelegate 中使用 if 语句?
Why cannot use if statement in AppDelegate?
为什么我不能在 if 语句中传递常量?
if UIDevice.current.orientation.isLandscape{
let storyboard = UIStoryboard(name: "landscape", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "landscape")
}else{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController")
}
self.window?.rootViewController = initialViewController
从作用域开始,您需要在 if else 块中插入错误行
if {
let initialViewController = ////
self.window?.rootViewController = initialViewController
}
else {
let initialViewController = ////
self.window?.rootViewController = initialViewController
}
或
var initialViewController:UIViewController!
if {
initialViewController = ////
}
else {
initialViewController = ////
}
// use it here
initialViewController
仅在if else
块的范围内可用,因此您需要在if else
块开始之前声明initialViewController
并设置initialViewController
属性 在块中,稍后根据需要使用它。
let initialViewController: UIViewController
if UIDevice.current.orientation.isLandscape {
let storyboard = UIStoryboard(name: "landscape", bundle: nil)
initialViewController = storyboard.instantiateViewController(withIdentifier: "landscape")
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController")
}
window?.rootViewController = initialViewController
注意:您可能应该对编程范围做更多研究。
仔细看Swift报错:在这部分代码中找不到名字为initialViewController
的变量
变量仅在创建它们的块范围内可用。换句话说,如果要在 if
语句之外的其他地方使用它,则需要将 initialViewController
声明从 if
语句中移除。您可以通过将其改为 var
来做到这一点:
// Let Swift know that we’re going to make this a UIViewController
var initialViewController: UIViewController
if condition {
// Now, initialViewController has been instantiated; it just needs a value
initialViewController = storyboard.instantiateViewController(withIdentifier: "landscape")
} else {
initialViewController = storyboard.instantiateViewController(withIdentifier: "portrait")
}
// We can safely use it outside of the if statement, but it may need unwrapping since it was declared as a var
self.window?.rootViewController = initialViewController!
请注意,copy/pasting 由于我尚未定义 storyboard
或 condition
变量,因此此确切代码将不起作用:看看您是否可以更改当前代码以修复按照我在这里描述的方式发布!
为什么我不能在 if 语句中传递常量?
if UIDevice.current.orientation.isLandscape{
let storyboard = UIStoryboard(name: "landscape", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "landscape")
}else{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController")
}
self.window?.rootViewController = initialViewController
从作用域开始,您需要在 if else 块中插入错误行
if {
let initialViewController = ////
self.window?.rootViewController = initialViewController
}
else {
let initialViewController = ////
self.window?.rootViewController = initialViewController
}
或
var initialViewController:UIViewController!
if {
initialViewController = ////
}
else {
initialViewController = ////
}
// use it here
initialViewController
仅在if else
块的范围内可用,因此您需要在if else
块开始之前声明initialViewController
并设置initialViewController
属性 在块中,稍后根据需要使用它。
let initialViewController: UIViewController
if UIDevice.current.orientation.isLandscape {
let storyboard = UIStoryboard(name: "landscape", bundle: nil)
initialViewController = storyboard.instantiateViewController(withIdentifier: "landscape")
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController")
}
window?.rootViewController = initialViewController
注意:您可能应该对编程范围做更多研究。
仔细看Swift报错:在这部分代码中找不到名字为initialViewController
的变量
变量仅在创建它们的块范围内可用。换句话说,如果要在 if
语句之外的其他地方使用它,则需要将 initialViewController
声明从 if
语句中移除。您可以通过将其改为 var
来做到这一点:
// Let Swift know that we’re going to make this a UIViewController
var initialViewController: UIViewController
if condition {
// Now, initialViewController has been instantiated; it just needs a value
initialViewController = storyboard.instantiateViewController(withIdentifier: "landscape")
} else {
initialViewController = storyboard.instantiateViewController(withIdentifier: "portrait")
}
// We can safely use it outside of the if statement, but it may need unwrapping since it was declared as a var
self.window?.rootViewController = initialViewController!
请注意,copy/pasting 由于我尚未定义 storyboard
或 condition
变量,因此此确切代码将不起作用:看看您是否可以更改当前代码以修复按照我在这里描述的方式发布!