Swift `in` keyword meaning?

后端 未结 3 483
离开以前
离开以前 2020-12-04 19:10

I am trying to implement some code from parse.com and I notice a keyword in after the void.

I am stumped what is this ? The second line you see the

相关标签:
3条回答
  • 2020-12-04 19:51

    In a named function, we declare the parameters and return type in the func declaration line.

    func say(s:String)->() {
        // body
    }
    

    In an anonymous function, there is no func declaration line - it's anonymous! So we do it with an in line at the start of the body instead.

    {
        (s:String)->() in
        // body
    }
    

    (That is the full form of an anonymous function. But then Swift has a series of rules allowing the return type, the parameter types, and even the parameter names and the whole in line to be omitted under certain circumstances.)

    0 讨论(0)
  • 2020-12-04 19:53

    Closure expression syntax has the following general form:

    0 讨论(0)
  • 2020-12-04 20:03

    *

    The start of the closure’s body is introduced by the in keyword. This keyword indicates that the definition of the closure’s parameters and return type has finished, and the body of the closure is about to begin.

    *

    Source: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

    0 讨论(0)
提交回复
热议问题