隐含地从'String?'强制表达了表达式去任何

时间:2016-11-19 09:20:37

标签: swift string optional

我有一些错误,例如“表达式隐式强制从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)

print(error?.localizedDescription)

请帮帮我

1 个答案:

答案 0 :(得分:37)

print函数需要一组Any个参数。 StringAny。在这种情况下,Xcode告诉您它隐式地将可选字符串强制转换为Any对象(通过转换String中的Optional(value)值)。

要避免此警告,您只需使用默认值或展开String?

即可
print(user?.email ?? "User instance is nil")
print(user!.email)