简体   繁体   English

Swift for In In循环遍历自定义类的数组不起作用

[英]Swift For In Loop Iterating Through an Array of Custom Classes Doesn't Work

I am working a project and have come across this error when iterating through a for in loop like this: 我正在工作一个项目,并在通过for in循环进行迭代时遇到了此错误:

class CustomClass {

    var nameNum : Int { didSet { self.name = "CustomClass \(nameNum)" } }
    var name : String

    init() {
        nameNum = 0
        self.name = "CustomClass \(nameNum)"
    }
}

var myArray : [CustomClass] = [CustomClass](repeating: CustomClass(), count: 5)

for _class in myArray.indices {
    myArray[_class].nameNum = _class
}

print("\n")
for _class in myArray.indices {
    print("Item \(_class): \(myArray[_class].name)")
}

I get the following output: 我得到以下输出:

Item 0: CustomClass 4 项目0:CustomClass 4

Item 1: CustomClass 4 项目1:CustomClass 4

Item 2: CustomClass 4 项目2:CustomClass 4

Item 3: CustomClass 4 第3项:CustomClass 4

Item 4: CustomClass 4 项目4:CustomClass 4

This does not make sense to me as I thought I would get the following output instead: 这对我来说没有意义,因为我认为我会得到以下输出:

Item 0: CustomClass 0 项目0:CustomClass 0

Item 1: CustomClass 1 项目1:CustomClass 1

Item 2: CustomClass 2 项目2:CustomClass 2

Item 3: CustomClass 3 第3项:CustomClass 3

Item 4: CustomClass 4 项目4:CustomClass 4

Any help as to why this doesn't work or to how to go about fixing it is appreciated, thanks! 感谢您提供任何有关为何无法解决问题或解决问题的帮助。

You should change your array initialization to 您应该将数组初始化更改为

var myArray : [CustomClass] = (0..<5).map { _ in CustomClass() }

from

var myArray : [CustomClass] = [CustomClass](repeating: CustomClass(), count: 5)

Complete Code: 完整代码:

class CustomClass {

    var nameNum : Int { didSet { self.name = "CustomClass \(nameNum)" } }
    var name : String

    init() {
        nameNum = 0
        self.name = "CustomClass \(nameNum)"
    }
}

var myArray : [CustomClass] = (0..<5).map { _ in CustomClass() }

for _class in myArray.indices {
    myArray[_class].nameNum = _class
}

print("\n")
for _class in myArray.indices {
    print("Item \(_class): \(myArray[_class].name)")
}

The reason is that your code, actually creates an instance of CustomClass and adds it at all 5 indexes of array where as you are expecting 5 different instance of CustomClass . 原因是您的代码实际上创建了一个CustomClass实例,并将其添加到数组的所有5个索引中,正如您所期望的那样,其中有5个不同的CustomClass实例。 If the same instance is added 5 times, then all of them will have the last set value which in your case is 4. 如果将同一实例添加5次,则所有实例的最后设置值均为4。

Item 0: CustomClass 0 项目0:CustomClass 0

Item 1: CustomClass 1 项目1:CustomClass 1

Item 2: CustomClass 2 项目2:CustomClass 2

Item 3: CustomClass 3 第3项:CustomClass 3

Item 4: CustomClass 4 项目4:CustomClass 4

Why are you setting nameNum to 0 in the initializer, if you're immediately going to change it? 如果要立即更改它,为什么在初始化器nameNum设置为0? Just combine the operations: 只需结合操作即可:

class CustomClass {
    var nameNum: Int
    var name: String { return "CustomClass \(nameNum)" }

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

var myArray = (0..<5).map(CustomClass.init(nameNum:))

for (index, element) in myArray.enumerated() {
    print("Item #\(index): \(element.name)")
}

Also, if name always has a value derived from nameNum , then it's better to have it be a computed property whose derived from nameNum , rather than having the responsibility of setting it fall to a side effect of setting nameNum . 同样,如果name始终具有从nameNum派生的值,那么最好将其作为一个从nameNum派生的计算属性,而不是将其设置nameNum于设置nameNum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM