如果让 - 多个条件

Tai*_*mal 5 ios swift swift2 option-type

我可以在 IF 中使用 let obj = Something 的多个条件吗

if let u = custom["u"] as? String || let url = custom["URL"] as? String
{
// Do something here
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*edo 4

您可以添加多个条件,但前提是使用 AND 条件。

如果您使用OR,您不知道哪个值被正确设置,因此无法正确访问变量。

通过AND测试,您可以确定这两个变量都已正确创建,因此您可以确定它们的类型。

您必须编写用逗号分隔符分隔的测试:

if let u = custom["u"], 
   let url = custom["URL"] {
    // Do something here
}
Run Code Online (Sandbox Code Playgroud)

此外,您可以 if let使用关键字直接在块之后添加一些条件where

if let u = custom["u"] where u == "testValue", 
   let url = custom["URL"] {
    // Do something here
}
Run Code Online (Sandbox Code Playgroud)