gpt4 book ai didi

swift - 更新数组中的对象

转载 作者:行者123 更新时间:2023-12-05 08:18:47 28 4
gpt4 key购买 nike

我有以下代码:

    struct Person {
var name: String
var age: Int
var check: Bool
}

var anotherPerson: Person
var people: [Person] = []

anotherPerson = Person(name: "Jan", age: 55, check: true)
people.append(anotherPerson)
anotherPerson = Person(name: "Diesel", age: 9, check: false)
people.append(anotherPerson)
anotherPerson = Person(name: "King", age: 3, check: false)
people.append(anotherPerson)

如何将国王的年龄从 3 岁更新为 4 岁?

最佳答案

好的,这对我有用。

if let index = people.index(where: {$0.name == "King"}) {
people[index].age = 4
}

关于swift - 更新数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49214752/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com