为什么Apple的闭包声明缺少参数标签 [英] Why Apple's closure declarations missing argument labels

查看:123
本文介绍了为什么Apple的闭包声明缺少参数标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 UIKit 时,大多数时候我看到闭包(作为函数参数)缺少这样的参数标签: func fooClosure(fooClosure:(Bool)-> Swift.Void)

As I read through the UIKit, most of the time I see closures (as a function parameter) with missing argument labels like this: func fooClosure(fooClosure: (Bool) -> Swift.Void)

在某些情况下,我

看一下 UIActivityViewController 的闭包之一(作为类型别名):

Take a look at one of UIActivityViewController's closure (as a type alias):

public typealias UIActivityViewControllerCompletionWithItemsHandler = (UIActivityType?, Bool, [Any]?, Error?) -> Swift.Void

3rd 类型代表什么?我必须查看代码的 Objective-C 版本,才能知道这些是 returnedItems

What does 3rd type stand for? I have to look at the Objective-C version of the code to know these are returnedItems.

为什么不这样实现:(请注意 activityItems 标签)

Why it is not implemented that way: (note the activityItems label)

public typealias UIActivityViewControllerCompletionWithItemsHandler = (UIActivityType?, Bool, _ activityItems: [Any]?, Error?) -> Swift.Void

编辑:
-是这只是一个临时状态(因为Swift尚未完全集成到这些框架中)?
没有参数标签的代码可读性较低。

推荐答案

问题是这是一个函数签名机器生成的typealias,机器没有按照您的意愿执行操作。考虑下面的Objective-C声明:

The problem is that this is a function signature typealias generated by machine, and the machine isn't doing what you would like it to do. Consider the following Objective-C declaration:

typedef void (^MyCoolType)(NSString* __nullable name);

Swift生成的界面是:

The Swift generated interface for that is:

public typealias MyCoolType = (String?) -> Swift.Void

如您所见,名称被剥夺;

在Swift中,没有什么可以阻止 you 定义带有内部标签的函数签名类型别名的。这是合法的:

Nothing stops you from defining a function signature typealias with internal labels, in Swift. This is legal:

public typealias MyCoolType = (_ name:String?) -> Void

但这并不是从Objective-C生成生成接口的方式。

But that does not happen to be how the generated interface is generated from Objective-C.

这篇关于为什么Apple的闭包声明缺少参数标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆