为什么我要强制转换 String?对于 Any,Xcode 会发出警告,但对于 AnyObject 则不会?
Why if I typecast String? to Any, Xcode gives off warning, but not with AnyObject?
此代码给出警告 "Expression implicitly coerced from 'String?' to Any."
let email : String?;
let password : String?;
let dict = ["email": email, "password": password] as [String: Any];
但是这段代码没有。
let email : String?;
let password : String?;
let dict = ["email": email, "password": password] as [String: AnyObject];
为什么?我怎样才能让 Any
不会因为 AnyObject
?
等选项的警告而打扰我
编辑:
这段代码也没有给出警告:
let email : String;
let password : String;
let dict = ["email": email, "password": password] as [String: Any];
但在这种情况下,我需要能够合并对象和可选性。似乎仅当变量类型都是对象 和 可选时才会出现警告。
根据 Swift Language Guide,当您将可选类型转换为 Any 时,您应该会收到警告(请参阅页面底部的注释)。您可以通过将可选值转换为 Any
来消除警告,如下所示。
let email : String?;
let password : String?;
let dict = ["email": email as Any, "password": password as Any] as [String: Any];
此代码给出警告 "Expression implicitly coerced from 'String?' to Any."
let email : String?;
let password : String?;
let dict = ["email": email, "password": password] as [String: Any];
但是这段代码没有。
let email : String?;
let password : String?;
let dict = ["email": email, "password": password] as [String: AnyObject];
为什么?我怎样才能让 Any
不会因为 AnyObject
?
编辑:
这段代码也没有给出警告:
let email : String;
let password : String;
let dict = ["email": email, "password": password] as [String: Any];
但在这种情况下,我需要能够合并对象和可选性。似乎仅当变量类型都是对象 和 可选时才会出现警告。
根据 Swift Language Guide,当您将可选类型转换为 Any 时,您应该会收到警告(请参阅页面底部的注释)。您可以通过将可选值转换为 Any
来消除警告,如下所示。
let email : String?;
let password : String?;
let dict = ["email": email as Any, "password": password as Any] as [String: Any];