加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 无法使用.last修改多维数组

发布时间:2020-12-14 02:24:58 所属栏目:百科 来源:网络整理
导读:这是我的代码: var states:[[[Int]]] // I create an empty multidimensional arraystates = [[[0,0],[0,0]]] // I give it a value// Why does here it doesn't work ? ('@ivalue $T11' is not identical to 'Int')states.last![0][0] = 1// And here it d
这是我的代码:
var states:[[[Int]]] // I create an empty multidimensional array
states = [[[0,0],[0,0]]] // I give it a value


// Why does here it doesn't work ? ('@ivalue $T11' is not identical to 'Int')
states.last![0][0] = 1

// And here it does ?
states[0][0][0] = 1

我不知道为什么它会在一个案例中引发错误而在另一个案例中不会引发错误?我以为它会做同样的事情……

last返回最后一个元素,但不允许您设置新值.实际上,该属性实现了get only:
/// The last element,or `nil` if the array is empty
var last: T? { get }

所以你不能用它来修改数组.

请注意,如果返回的元素是复合值类型(即结构,如数组或字典),则返回存储在数组中的实际元素的副本.因此,对最后一个或任何属性和数据返回的元素所做的任何更改仅在该副本上执行,而不会影响原始数组.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读