让非可变数组迅速 [英] let Non-mutable array in swift

查看:71
本文介绍了让非可变数组迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let 类型变量应该是常量,但是以后重新分配值不会引发任何错误.

let type variable are supposed to be constants, but reassign a value later does not throw any error.

如何创建一个非可变数组.

How do I create a non-mutable array.

  let testArray = ["Catfish","water","tulips"]
  print(testArray[1])
  testArray[1] = "bottle of water"
  print(testArray[1])

输出是一瓶水"

推荐答案

您不是在重新声明数组本身,而是在重新声明数组的元素.在Swift中是允许的.当前没有一种使数组中所有元素不变的方法.不过,应该在未来更新中进行更改.

You aren't reassinging the array itself, but an element of the array. This is allowed in Swift. There isn't currently a way to make all of the elements in the array immutable. It should be changed in a future update though.

您可能还想看看

You may also want to take a look at this. Arrays have interesting assignment and copying behavior in Swift.

这篇关于让非可变数组迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆