【问题标题】:Why can I alter the members of a const array in Swift? [duplicate]为什么我可以在 Swift 中更改 const 数组的成员? [复制]
【发布时间】:2014-06-08 15:55:34
【问题描述】:

为什么下面的代码有效?试图弄清楚 Swift 如何处理 const。

let list = [1, 2, 3, 4, 5]
list[1] = 0

编辑:这是Is there a reason that Swift array assignment is inconsistent (neither a reference nor a deep copy)? 的副本

我需要在搜索方面做得更好:)

【问题讨论】:

    标签: swift


    【解决方案1】:

    在 Swift 中数组是 const 有点不同于 Swift 中的其他东西。当一个数组为const时,它只是意味着它的长度不能改变,单个元素仍然可以改变。

    【讨论】:

      【解决方案2】:

      要使用 C 等效项,list 就像“指向非常量对象的常量指针”。这意味着您无法更改list 指向的数组,但您可以更改其内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-11
        • 1970-01-01
        • 1970-01-01
        • 2018-09-06
        • 2017-10-11
        • 2020-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多