Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 表达式从';字符串?';对任何人_Swift_String_Optional - Fatal编程技术网

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)