更改数组中struct的值

rez*_*a23 52 arrays struct swift

我想将结构存储在数组中,访问并更改for循环中结构的值.

struct testing {
    var value:Int
}

var test1 = testing(value: 6 )

test1.value = 2
// this works with no issue

var test2 = testing(value: 12 )

var testings = [ test1, test2 ]

for test in testings{
    test.value = 3
// here I get the error:"Can not assign to 'value' in 'test'"
}
Run Code Online (Sandbox Code Playgroud)

如果我将结构更改为类它可以工作.任何人都可以告诉我如何更改结构的值.

Ant*_*nio 78

除了@MikeS所说的,请记住结构是值类型.所以在for循环中:

for test in testings {
Run Code Online (Sandbox Code Playgroud)

数组元素的副本分配给test变量.您对其所做的任何更改都仅限于该test变量,而不对数组元素进行任何实际更改.它适用于类,因为它们是引用类型,因此引用而不是被复制到test变量.

正确的方法是使用forby索引:

for index in 0..<testings.count {
    testings[index].value = 15
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您正在访问(和修改)实际的struct元素而不是它的副本.


Den*_*kin 13

如何Array改变Structs

对于每个元素:

itemsArray.indices.forEach { itemsArray[$0].someValue = newValue }
Run Code Online (Sandbox Code Playgroud)

对于特定元素:

itemsArray.indices.filter { itemsArray[$0].propertyToCompare == true }
                  .forEach { itemsArray[$0].someValue = newValue }
Run Code Online (Sandbox Code Playgroud)


Lig*_*Man 9

好吧,我将更新我对swift 3兼容性的回答.

当您编程很多时,您需要更改集合中对象的某些值.在这个例子中,我们有一个struct数组,并给出了一个我们需要改变特定对象值的条件.这在任何开发日都很常见.

而不是使用索引来确定哪个对象必须被修改,我更喜欢使用if条件,恕我直言更常见.

import Foundation

struct MyStruct: CustomDebugStringConvertible {
    var myValue:Int
    var debugDescription: String {
        return "struct is \(myValue)"
    }
}

let struct1 = MyStruct(myValue: 1)
let struct2 = MyStruct(myValue: 2)
let structArray = [struct1, struct2]

let newStructArray = structArray.map({ (myStruct) -> MyStruct in
    // You can check anything like:
    if myStruct.myValue == 1 {
        var modified = myStruct
        modified.myValue = 400
        return modified
    } else {
        return myStruct
    }
})

debugPrint(newStructArray)
Run Code Online (Sandbox Code Playgroud)

注意所有的让,这种开发方式更安全.

类是引用类型,不需要复制以更改值,就像结构一样.对类使用相同的示例:

class MyClass: CustomDebugStringConvertible {
    var myValue:Int

    init(myValue: Int){
        self.myValue = myValue
    }

    var debugDescription: String {
        return "class is \(myValue)"
    }
}

let class1 = MyClass(myValue: 1)
let class2 = MyClass(myValue: 2)
let classArray = [class1, class2]

let newClassArray = classArray.map({ (myClass) -> MyClass in
    // You can check anything like:
    if myClass.myValue == 1 {
        myClass.myValue = 400
    }
    return myClass
})

debugPrint(newClassArray)
Run Code Online (Sandbox Code Playgroud)


Ant*_*ich 7

为了简化使用数组中的值类型,您可以使用以下扩展(Swift 3):

extension Array {
    mutating func modifyForEach(_ body: (_ index: Index, _ element: inout Element) -> ()) {
        for index in indices {
            modifyElement(atIndex: index) { body(index, &$0) }
        }
    }

    mutating func modifyElement(atIndex index: Index, _ modifyElement: (_ element: inout Element) -> ()) {
        var element = self[index]
        modifyElement(&element)
        self[index] = element
    }
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

testings.modifyElement(atIndex: 0) { $0.value = 99 }
testings.modifyForEach { $1.value *= 2 }
testings.modifyForEach { $1.value = $0 }
Run Code Online (Sandbox Code Playgroud)