arrays - var数组和let数组的区别?
问题描述
我正在研究 Swift 中的数组,在我的书中他们首先写道:
let numbers = [0, 1, 2, 3]
但然后写:
var numbers = [0, 1, 2, 3]
我知道它let
表示常量并var
引用变量,但实际上声明为常量的数组和声明为变量的数组有什么变化?
解决方案
因为 swift 中structs
的数组声明了一个数组,let
它不仅会阻止您为其分配新值,还会阻止您更改其内容
例如:
let arr = [0, 1, 2]
arr[0] = 10 //will not compile
arr = [] //will not compile
推荐阅读
- observable - Angular 6,Rxjs 6,使用 HttpClient 处理登录
- angular - Angular 6 - 将日期字符串与数组过滤器上的日期进行比较
- c++ - 绑定到可变成员函数
- android - 保持 Intent 服务运行?
- sql-server - TSQL 强制转换视图定义中的所有列
- vue.js - ASP.NET Core SPA Vue 模板与 Vue CLI
- c++ - 具有 unique_ptr 的 CRTP 导致段错误
- python - Python-Sphinx:文档字符串代码测试以及通过 IPython 指令运行代码?
- opencv - Tensorflow如何从OpenCV图像帧中读取张量
- java - Android Studios:点击按钮解锁屏幕方向