简体   繁体   English

在变量中使用闭包时,swift变量和函数有什么区别?

[英]What is the difference between swift variable and function when use closure in variable?

Swift closure is confusing me when using in variable. 当在变量中使用时,Swift closure让我感到困惑。 Consider the following example 请考虑以下示例

let divide = {(val1: Int, val2: Int) -> Int in 
   return val1 / val2 
}
let result = divide(200, 20)
println(result)

Here divide is a variable but it can takes parameters. 这里divide是一个变量,但它可以带参数。 I know from other language, only function can take parameters. 我从其他语言知道,只有函数可以参数。 So, what is the difference between variable and function? 那么,变量和函数之间有什么区别? What is the advantage when use clousure in swift variable? 在swift变量中使用clousure什么好处?

Simply Closure is an block of code (anonymous functions) that u can use it as an object and pass it around between viewcontroller, its good if u want to do something after the function is complete or after pressed a button (can achieve the same with protocol/delegate, unwind, ...), it also can take params like a function and return value Simply Closure是一个代码块(匿名函数),你可以将它作为一个对象使用并在viewcontroller之间传递它,如果你想在函数完成后或按下一个按钮之后做一些事情,那就很好(可以实现同样的功能) protocol / delegate,unwind,...),它也可以像函数和返回值一样使用参数

The great thing is that it can let u access non-local variable (when u pass it to another viewcontroller) then u can do stuff with it 最棒的是它可以让你访问非局部变量(当你把它传递给另一个viewcontroller时)然后你可以用它来做它的东西

in your example, it doesnt really highlight the difference between closures and functions. 在你的例子中,它并没有真正突出闭包和函数之间的区别。 closures and functions are very similar. 闭包和功能非常相似。 like how a function can take in parameters, or use variables outside of the function if they are defined in the class, a closure can do the same thing, but from within its own local scope. 就像一个函数如何接受参数,或者如果在类中定义它们时使用函数之外的变量,闭包可以做同样的事情,但是从它自己的局部范围内。

psuedo code example: 伪代码示例:

class aaa {

  var a = 5;

  func foo (b:Int) -> Int {
     return a+b;  //a not defined in local scope, but we can still use it
  }
}

now with a closure its the same thing, but you can use local variables in the same way a variable defined at the class level is used 现在有一个闭包它同样的东西,但你可以使用局部变量,就像使用在类级别定义的变量一样

class bbb {

  var b = 1;
  var closure;

  func foo (c:Int) -> Int {
     var d = b+c; //d is defined locally
     closure = (val1:Int) -> Int { Int in
       return val1 + b * d; //we can use b and d here! they are captured in the block now, and will remain with the block until executed
     }
  }

  func bar () {

    b = 3;

    closure(5); //now we execute closure, now the variable d in foo() is being used but in a completely different scope, and even if we changed the value of b before running it, the value of b would be the original value captured in the closure when it was made aka 1 and not 3
  }
}

so now if you ran foo then bar, you can see how closures differ to functions 所以现在如果你运行foo然后bar,你可以看到闭包与函数的区别

so the real beauty is in the capturing of variables locally, to be used at a later time in possibly a completely different scope, where as functions can only use the parameters you give them and/or the variables defined in the class 所以真正的美在于本地捕获变量,以后可能在一个完全不同的范围内使用,因为函数只能使用你给它们的参数和/或类中定义的变量

A variable holds/references a value; 变量保存/引用值; a function is a value. 函数是一个值。 Thus variables can hold/reference numbers, strings, instances and, in some languages, functions. 因此,变量可以包含/引用数字,字符串,实例以及某些语言的函数。 Languages that allow variables to hold/reference functions are languages with ' first class functions ' 允许变量保持/引用函数的语言是具有“ 第一类函数 ”的语言

The advantage of closures is that they 'capture' values in their ' lexical scope '. 闭包的优点在于它们在“ 词法范围 ”中“捕获”了价值。 For example, say you need a callback function that executes when some computation completes. 例如,假设您需要一个在某些计算完成时执行的回调函数。 You want the callback to dismiss a view controller and log some information: 您希望回调解除视图控制器并记录一些信息:

class MyViewController : ViewController {
  func takeAction (message:String) {
    // do some stuff, then
    doComputation { 
      self.dismissViewController()
      NSLog ("took action: \(message)")
    }
  }
}   

In the above, the argument to doComputation is a closure; 在上面, doComputation的参数是一个闭包; it captures the values bound to self and message from the closure's lexical environment. 它从闭包的词汇环境中捕获绑定到selfmessage的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM