Swift3 在Swift 3的新api设计指南中,后续闭包是否会过时?
我正在将一个项目转换为Swift 3,并挑选已经转换为Swift 3的库。在这个过程中,我遇到了这个示例,其中作者代码()将方法调用从Swift3 在Swift 3的新api设计指南中,后续闭包是否会过时?,swift3,Swift3,我正在将一个项目转换为Swift 3,并挑选已经转换为Swift 3的库。在这个过程中,我遇到了这个示例,其中作者代码()将方法调用从 addTextFieldWithConfigurationHandler(configurationHandler: (UITextField -> Void)? = nil) 致: 基本上是将withHandler部分从方法名称中移到参数标签中 似乎这样做是为了符合新的标准 因此,在Swift 2.x中,呼叫可能如下所示: alertControlle
addTextFieldWithConfigurationHandler(configurationHandler: (UITextField -> Void)? = nil)
致:
基本上是将withHandler部分从方法名称中移到参数标签中
似乎这样做是为了符合新的标准
因此,在Swift 2.x中,呼叫可能如下所示:
alertController.addTextFieldWithConfigurationHandler() { tf in
tf.placeholder = "email id"
tf.keyboardType = .EmailAddress
self.changePasswordForEmailId = tf
}
而在Swift 3(或者更确切地说是库的更新版本)中,它看起来是这样的:
alertController.addTextField(withHandler: { tf in
tf.placeholder = "email id"
tf.keyboardType = .emailAddress
self.changePasswordForEmailId = tf
})
或者这个:
alertController.addTextField { tf in
tf.placeholder = "email id"
tf.keyboardType = .emailAddress
self.changePasswordForEmailId = tf
}
因此,在本例中,如果您希望使用包含闭包角色(处理程序)的更具描述性的形式,您将松开尾随闭包语法,而使用尾随闭包语法将松开有关闭包的角色信息
在我看来,在这样的情况下,方法名和尾随闭包实际上是三个选项中最好的
我没有注意到在新的报告中有任何内容专门涉及这样的案例,我是否遗漏了什么?在这种情况下,是否有人能指出一些文档会显示对特定实现的偏好
alertController.addTextField { tf in
tf.placeholder = "email id"
tf.keyboardType = .emailAddress
self.changePasswordForEmailId = tf
}