swift - if let 可以在 Swift 3.1 中以逗号分隔吗?

标签 swift if-statement

Implement Navigation开始篇Developing iOS Apps (Swift) Apple的教程,有以下代码片段:

if let sourceViewController = sender.sourceViewController as? MealViewController,
   meal = sourceViewController.meal {}

本教程使用 Swift 3.1,此代码段似乎对我不起作用。仅当我在 meal = sourceViewController.meal 前面加上 let 关键字时它才有效,如下所示:

if let sourceViewController = sender.sourceViewController as? MealViewController,
   let meal = sourceViewController.meal {}

我的问题纯粹出于好奇:if let a = b, c = d 在任何 Swift 版本中是否曾经是有效语法?

最佳答案

是的,以下代码使用 Xcode 8 和“传统模式”(即 swift 2.3):

let a: Int? = 1
let b: Int? = 2

if let x = a, y = b { }

从 Swift 3 开始,它必须写成

if let x = a, let y = b { }

由于 SE-0099 Restructuring Condition Clauses ,特别是(强调):

The root problem lies in the condition grammar: commas are used both to separate items within a clause (e.g. in if let x = a, y = b {) and to separate mixed kinds of clauses (e.g. if let x = a, case y? = b {). This proposal resolves this problem by retaining commas as separators between clauses (as used elsewhere in Swift) and limits clauses to single items.

关于swift - if let 可以在 Swift 3.1 中以逗号分隔吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45258258/

相关文章:

if-statement - Pascal if/else 程序语法错误

java - PaintComponent 内部的 if 语句使其不绘制

c++ - 错误 : two or more data types in declaration of main?

c - 服务器不会进入 if 语句,它会构造。用 C 语言进行套接字编程

ios - 使用 SwiftyDropbox 创建指向文件的共享链接

swift - “AnyObject”不是 'Custom Tuple' 的子类型

swift - 无法将数据从 tableViewController 传递到嵌入 ViewController 中的另一个 TableViewController

ios - 如何调整 UITableView 宽度以适应其内容?

ios - NSObject 在加载 viewDidLoad() 之前似乎没有初始化

Mysql 在 IF 条件内选择