解决Swift 3中的非转义闭包问题
发布时间:2020-12-14 04:50:58 所属栏目:百科 来源:网络整理
导读:我有一个扩展数组的形式: extension Array{ private func someFunction(someClosure: (() - Int)?) { // Do Something } func someOtherFunction(someOtherClosure: () - Int) { someFunction(someClosure: someOtherClosure) }} 但我收到错误:将非转义参
我有一个扩展数组的形式:
extension Array { private func someFunction(someClosure: (() -> Int)?) { // Do Something } func someOtherFunction(someOtherClosure: () -> Int) { someFunction(someClosure: someOtherClosure) } } 但我收到错误:将非转义参数’someOtherClosure’传递给函数,期望@escaping闭包. 两个闭包确实是非转义(默认情况下),并且明确地将@noescape添加到someFunction会产生一个警告,指示这是Swift 3.1中的默认值. 知道我为什么会收到这个错误吗? – 更新 – 解决方法
如前所述,可选闭包正在逃避.另外一个补充:
Swift 3.1有一个withoutActuallyEscaping辅助函数,在这里很有用.它标记了一个闭包,只是为了在传递的闭包内使用它,因此您不必将转义属性公开给函数签名. 可以像这样使用: extension Array { private func someFunction(someClosure: (() -> Int)?) { someClosure?() } func someOtherFunction(someOtherClosure: () -> Int) { withoutActuallyEscaping(someOtherClosure) { someFunction(someClosure: $0) } } } let x = [1,2,3] x.someOtherFunction(someOtherClosure: { return 1 }) 希望这有用! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- c – void Classname :: operator()(){…}做什么
- org.xml.sax.SAXException: SimpleDeserializer
- c# – 在VS10中调试时不会打中断点
- c# – 如何在linq join(lambda)上添加where子句?
- c# – 如何在Windows Phone 7中以编程方式创建Li
- c – 三元运算符的结果不是右值
- 第二部分:如何使Ruby AES-256-CBC和PHP MCRYPT_
- MSBuild – 如何从预先写入的命令行命令构建.NET
- ruby – Nokogiri 1.5.9 gem不能安装在Mac OS X
- ruby-on-rails-3 – 在route.rb中的某些路由中添
热点阅读