码迷,mamicode.com
首页 > 其他好文 > 详细

functions and closures are reference types-函数和闭包是引用类型

时间:2018-12-29 21:11:07      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:contents   amp   var   lines   org   pes   cut   sel   call   

Closures Are Reference Types

In the example above, incrementBySeven and incrementByTen are constants, but the closures these constants refer to are still able to increment the runningTotal variables that they have captured. This is because functions and closures are reference types.

Whenever you assign a function or a closure to a constant or a variable, you are actually setting that constant or variable to be a reference to the function or closure. In the example above, it is the choice of closure that incrementByTen refers to that is constant, and not the contents of the closure itself.

This also means that if you assign a closure to two different constants or variables, both of those constants or variables refer to the same closure.

  1. let alsoIncrementByTen = incrementByTen
  2. alsoIncrementByTen()
  3. // returns a value of 50
  4. incrementByTen()
  5. // returns a value of 60

The example above shows that calling alsoIncrementByTen is the same as calling incrementByTen. Because both of them refer to the same closure, they both increment and return the same running total.

 

https://docs.swift.org/swift-book/LanguageGuide/Closures.html

functions and closures are reference types-函数和闭包是引用类型

标签:contents   amp   var   lines   org   pes   cut   sel   call   

原文地址:https://www.cnblogs.com/feng9exe/p/10197637.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!