博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
functions and closures are reference types-函数和闭包是引用类型
阅读量:6721 次
发布时间:2019-06-25

本文共 1133 字,大约阅读时间需要 3 分钟。

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

转载地址:http://yzjmo.baihongyu.com/

你可能感兴趣的文章
LeetCode OJ - Gas Station
查看>>
MacOSX中降频的方法
查看>>
audio音频暂停和播放
查看>>
Windows下安装Python requests模块
查看>>
java图形用户界面边界布局管理器
查看>>
java web 程序---注册页面密码验证
查看>>
Linux修改环境变量步骤
查看>>
PyQt4 / PyQt5
查看>>
使用vue开发输入型组件更好的一种解决方式(子组件向父组件传值,基于2.2.0)
查看>>
linux 服务器安装 nginx
查看>>
108. Convert Sorted Array to Binary Search Tree
查看>>
Android 学习开发笔记《Service 与 Thread 的区别 》
查看>>
grep:Binary file (standard input) matches
查看>>
云计算
查看>>
centos7.2下部署 python3
查看>>
Shell 编程(实例一)
查看>>
C++Primer笔记——文本查询程序(原创,未使用类)
查看>>
Matplotlib 知识点整理
查看>>
Django问题 TypeError: __init__() missing 1 required positional argument: 'on_delete'
查看>>
面向对象(上)之一
查看>>