相关疑难解决方法(0)

Swift 2 - "if"中的模式匹配

最近我看到了Apple的2015年WWDC主题演讲.我也查看了一些文档,但是我找不到"if if pattern"部分,它是如何写在他们展示的幻灯片上的.(来自Apple Events的 68分00秒视频)

你知道这是指什么吗?还是语法?

syntax if-statement pattern-matching swift swift2

32
推荐指数
1
解决办法
1万
查看次数

swift 2中引入的可选模式的优点/用例有哪些?

对于简单的情况,if let或者guard我没有看到优势,

if case let x? = someOptional where ... {
  ...
}

//I don't see the advantage over the original if let

if let x = someOptional where ... {
  ...
}
Run Code Online (Sandbox Code Playgroud)

对于for-case-let简化使用可选集合的情况,我真的希望swift可以更进一步:

for case let x? in optionalArray {
  ...
}

//Wouldn't it be better if we could just write

for let x? in optionalArray {
  ...
}
Run Code Online (Sandbox Code Playgroud)

谷歌一段时间之后我认为唯一有用的案例是" Swift 2模式匹配:解开多个选项 ":

switch (username, password) {
case let (username?, password?):
    print("Success!")
case …
Run Code Online (Sandbox Code Playgroud)

pattern-matching switch-statement optional swift swift2

4
推荐指数
1
解决办法
295
查看次数