Swift 表达式从';字符串?';对任何人
我有一些错误,比如“表达式从Swift 表达式从';字符串?';对任何人,swift,string,optional,Swift,String,Optional,我有一些错误,比如“表达式从String?隐式强制到Any”这是我的代码: func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch.
String?
隐式强制到Any
”这是我的代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FIRApp.configure()
FIRAuth.auth()?.signIn(withEmail: "myemail@gmail.com", password: "mypassword", completion: { (user, error) in
if (error != nil) {
print(user?.email)
}else {
print(error?.localizedDescription)
}
})
return true
}
这行有错误
print(user?.email)
及
请帮助我打印
功能需要一组任何参数<代码>字符串
是一个任意
。在本例中,Xcode告诉您它隐式地将可选字符串强制为Any
对象(通过在optional(value)
中转换string
值)
要避免此警告,只需使用默认值或打开字符串?
print(user?.email ?? "User instance is nil")
print(user!.email)
如果第二行的用户为零,那么会发生什么?当然,应用程序会崩溃。但在我解释了可选性之后,我认为每次都要写是多么愚蠢的工作啊??“至少你的应用程序会因为指纹而崩溃。为什么它不能在变成零的地方打印出零,就这样?@Zaporozhchenkokaleksandr我完全同意你的观点,但我的回答范围是解释为什么会发生错误以及如何消除它。“最好的方法就是找到它。”卢卡德·阿尔贝蒂:是的,当然。这只是对本机api的抱怨;)感谢您所做的一切您可以做user?。以任何方式发送电子邮件
,明确说明您希望将其转换为Any
print(user?.email ?? "User instance is nil")
print(user!.email)