swift函数调用:自我关键字vs无 [英] swift function calls: self-keyword vs without

查看:81
本文介绍了swift函数调用:自我关键字vs无的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  self.myFuction(

我想知道这两个函数在类中是否有区别。 )

VS

  myFunction()

它正在以两种方式工作。是否有必要使用self-keyword?

解决方案

在大多数情况下:绝对没有区别。但是如果你省略自我,则更加快速。
但是有一种情况,当你必须在闭包表达式中使用self时:



但自从Swift 1.2开始,使用@noescape参数,您可以在关闭中省略self。

I was wondering if there is any difference between those two a function-calls in a class:

self.myFuction()

VS

myFunction()

it is working in both ways. Is there the case where it's necessary to use the self-keyword?

解决方案

In most cases: there is absolutely no difference. But it's more "swiftish" if you omit "self". But there is a case, when you have to use self: in closure expressions.

But since Swift 1.2, with the @noescape parameter, you can omit "self" in closures as well.

这篇关于swift函数调用:自我关键字vs无的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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