是否必须使用“in”?闭包中的关键字?如果不是,那么 swift 中的闭包和计算属性之间的语法差异是什么?
在 swift 中,get 和 set 的使用不是强制性的,如果在闭包中使用“in”也不是强制性的,那么如何区分闭包和计算属性呢?
就像下面的例子中的greet是一个闭包还是计算属性?
var greet = {
return 4+3
}
greet()
In swift use of get and set is not compulsory and if use of "in" in closure is also not compulsory then how to differentiate a closure and computed property?
Like in below example greet is a closure or computed property?
var greet = {
return 4+3
}
greet()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
Saygoodbye2025-01-24 02:13:19
greet
是一个闭包。计算属性是
var greet : Int {
return 4+3
}
greet // without parentheses
如果传递参数,闭包中的“in”也不是强制(顺便说一句,return
关键字不是强制的)
var greet = { x in
4+x
}
greet(4)
除非您使用简写语法
var greet = {
4+$0
}
greet(4)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当需要传递参数时,可以使用关键字
in
。函数和计算属性之间也存在差异:如果使用符号
=
,则将变量等同于函数,因此需要调用greet()
。如果您使用
:
而不是=
,则您有一个计算属性,请调用greet
。以下是不同案例的列表:
You use the keyword
in
when you need to pass a parameter.There are also differences between functions and computed properties: if you use the symbol
=
, you are equalling your variable to a function, so you need to callgreet()
.If instead
=
you use:
, you have a computed property, callgreet
.Here's a list of different cases: