无法转换类型为';任何';到指定类型';Bool';-Xcode、Swift、iOS错误
我得到了这个错误: : 有人知道如何解决这个问题吗?谢谢大家!无法转换类型为';任何';到指定类型';Bool';-Xcode、Swift、iOS错误,swift,xcode,ios13,swift5,Swift,Xcode,Ios13,Swift5,我得到了这个错误: : 有人知道如何解决这个问题吗?谢谢大家! var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { FirebaseApp.configure() let hasSession
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
let hasSession = UserDefaults.standard.value(forKey: "UserHasSubmittedPassword") ?? false
let vc: UIViewController = {
if hasSession {
// next vc you want to show
} else {
// enter password vc
}
}()
let navigationController = UINavigationController(rootViewController: vc)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
UserDefaults类根据其类型具有检索对象的特定方法 对于布尔人,您可以使用:
UserDefaults.standard.bool(forKey:“UserHasSubmittedPassword”)
从文件中:
此方法自动强制某些“真实”值,例如
字符串“true”、“YES”和“1”,以及数字1和1.0-到
布尔值为true。某些“虚假”的价值观也是如此,比如
作为字符串“false”、“NO”和“0”,以及数字0和0.0-
将自动强制为布尔值false
以及:
与指定键关联的布尔值。如果指定
键不存在,此方法返回false
这意味着您也不需要在示例代码中使用nil合并运算符?
,因为通过使用方法UserDefaults.standard.bool(forKey:)
,如果密钥不存在,则已经返回false
因此,在示例代码中,您可以使用:
let hasSession: Bool = UserDefaults.standard.bool(forKey: "UserHasSubmittedPassword")
若您还对读取其他类型的值感兴趣,UserDefaults类还具有读取字符串、URL、数组、字典、Int、Float、Double等的特定方法
完整的列表可以在这里找到:@foodreams您可以试试这个,希望它能帮助您
UserDefaults.standard.set(true, forKey: "yourkeyname")
UserDefaults.standard.synchronize()
func setupRootVC()
{
var rootVC = UIViewController()
if (UserDefaults.standard.value(forKey: "yourKey") as? Bool) != nil
{
rootVC = UIStoryboard.Dashboard.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
}
else
{
rootVC = UIStoryboard.Login.instantiateViewController(withIdentifier: "ChangePasswordVC") as! ChangePasswordVC
}
let rootNC = UINavigationController(rootViewController: rootVC)
//all your further code
}
UserDefaults
中使用value(forKey:
)来获取单个值。有对象(forKey:
和用于标量类型的特殊API,如bool(forKey:)
、整数(forKey:)
和double(forKey:)
由于vc
用作局部变量,因此使用闭包语法的声明不合适
直接检查UserDefaults
中的布尔值即可
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
let vc : UIViewController
if UserDefaults.standard.bool(forKey: "UserHasSubmittedPassword") {
vc = <next vc you want to show>
} else {
vc = <enter password vc>
}
let navigationController = UINavigationController(rootViewController: vc)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions键:任意])->Bool{
FirebaseApp.configure()
让vc:UIViewController
如果UserDefaults.standard.bool(forKey:“UserHasSubmittedPassword”){
vc=
}否则{
vc=
}
让navigationController=UINavigationController(rootViewController:vc)
window?.rootViewController=navigationController
窗口?.makeKeyAndVisible()
返回真值
}
在密码vc被解除后,不要忘记为key
UserHasSubmittedPassword
保存true
。发布你的代码,而不是链接图像。嗨,我复制/粘贴了你的代码,但我在sideHi@Anita上没有找到提到的错误。现在我得到了一个不同的错误:“预期返回‘UIViewController’的闭包中缺少返回”imgur.com/a/gec5ez7Yes,因为您没有返回要显示的视图控制器,例如让vc:UIViewController={如果hasSession{//写下要显示的控制器(我正在使用xib,我正在传递视图控制器)return UIViewController.init(nibName:“UIViewController”,bundle:nil)}else{return UIViewController.init(nibName:“UIViewController”,bundle:nil)}}()只需在hasSession和else部分中返回所需的视图控制器,谢谢!但随后出现以下错误: