“无法分配”错误迭代结构数组

时间:2022-06-03 23:59:56

I have an array of structs:

我有一系列结构:

struct CalendarDate {
    var date: NSDate?
    var selected = false
}

private var collectionData = [CalendarDate]()

Which I simply populate with a date like this:

我简单地用这样的日期填充:

    for _ in 1...7 {
        collectionData.append(CalendarDate(date: NSDate(), selected: false))
    }

So when you tap on a collectionView, I simply want to loop through the data and mark them all as False.

因此,当您点击collectionView时,我只想循环遍历数据并将它们全部标记为False。

    for c in collectionData {
        c.selected = false  ///ERROR: Cannot assign to 'selected' in 'c'
    }

Why do I get this error?

为什么我会收到此错误?

If I do this, it works fine but I want to know what I did wrong above:

如果我这样做,它工作正常,但我想知道我上面做错了什么:

    for i in 0..<collectionData.count {
        collectionData[i].selected = false
    }

2 个解决方案

#1


12  

As I understand it, the iterator

据我所知,迭代器

for c in collectionData

for collectionData中的c

returns copies of the items in collectionData - (structs are value types, not reference types, see http://www.objc.io/issue-16/swift-classes-vs-structs.html), whereas the iteration

返回collectionData中项目的副本 - (结构是值类型,而不是引用类型,请参阅http://www.objc.io/issue-16/swift-classes-vs-structs.html),而迭代

for i in 0..<collectionData.count

因为我在0 ..

accesses the actual values. If I am right in that, it is pointless to assign to the c returned from the iterator... it does not "point" at the original value, whereas the

访问实际值。如果我是正确的,那么分配给迭代器返回的c是没有意义的......它没有“指向”原始值,而

collectionData[i].selected = false

collectionData [i] .selected = false

in the iteration is the original value.

在迭代中是原始值。

Some of the other commentators suggested

一些其他评论员建议

for (var c) in collectionData

for collectionData中的(var c)

but although this allows you to assign to c, it is still a copy, not a pointer to the original, and though you can modify c, collectionData remains untouched.

但是虽然这允许你分配给c,它仍然是一个副本,而不是指向原始的指针,虽然你可以修改c,但collectionData保持不变。

The answer is either A) use the iteration as you originally noted or B) change the data type to a class, rather than a struct.

答案是:A)使用最初记录的迭代或B)将数据类型更改为类,而不是结构。

#2


5  

because each 'c' is by default let, and this is a new instance of CalendarDate and the value of array at index copied to this for each step of for, and 'c' isn't pointer to the index of the array and it is just a copy of index, so if you set a new value to this, the new value does not apply in array. but 'i' is used as index of array and can directly manipulate the values of array.

因为默认情况下每个'c'都是let,这是CalendarDate的一个新实例,并且索引处的数组值为for的每一步复制到此,'c'不是指向数组索引的指针只是索引的副本,因此如果为此设置新值,则新值不适用于数组。但'i'用作数组的索引,可以直接操作数组的值。

#1


12  

As I understand it, the iterator

据我所知,迭代器

for c in collectionData

for collectionData中的c

returns copies of the items in collectionData - (structs are value types, not reference types, see http://www.objc.io/issue-16/swift-classes-vs-structs.html), whereas the iteration

返回collectionData中项目的副本 - (结构是值类型,而不是引用类型,请参阅http://www.objc.io/issue-16/swift-classes-vs-structs.html),而迭代

for i in 0..<collectionData.count

因为我在0 ..

accesses the actual values. If I am right in that, it is pointless to assign to the c returned from the iterator... it does not "point" at the original value, whereas the

访问实际值。如果我是正确的,那么分配给迭代器返回的c是没有意义的......它没有“指向”原始值,而

collectionData[i].selected = false

collectionData [i] .selected = false

in the iteration is the original value.

在迭代中是原始值。

Some of the other commentators suggested

一些其他评论员建议

for (var c) in collectionData

for collectionData中的(var c)

but although this allows you to assign to c, it is still a copy, not a pointer to the original, and though you can modify c, collectionData remains untouched.

但是虽然这允许你分配给c,它仍然是一个副本,而不是指向原始的指针,虽然你可以修改c,但collectionData保持不变。

The answer is either A) use the iteration as you originally noted or B) change the data type to a class, rather than a struct.

答案是:A)使用最初记录的迭代或B)将数据类型更改为类,而不是结构。

#2


5  

because each 'c' is by default let, and this is a new instance of CalendarDate and the value of array at index copied to this for each step of for, and 'c' isn't pointer to the index of the array and it is just a copy of index, so if you set a new value to this, the new value does not apply in array. but 'i' is used as index of array and can directly manipulate the values of array.

因为默认情况下每个'c'都是let,这是CalendarDate的一个新实例,并且索引处的数组值为for的每一步复制到此,'c'不是指向数组索引的指针只是索引的副本,因此如果为此设置新值,则新值不适用于数组。但'i'用作数组的索引,可以直接操作数组的值。