我有一个简单的数组,想遍历它,并用false替换所有元素,但是for循环有问题。下面是我所做的一个示例。我是Swift的新手,因此真诚地感谢您对循环外观的任何帮助。
order by len([Forecast Week]) desc, [Forecast Week] desc;
答案 0 :(得分:1)
您正在尝试:
for _ in sampleMembers() { _ = false }
_
表示您希望忽略该值。您不想忽略该值,需要它。
for member in sampleMembers() { member.active = false }
但这有两个问题。 member
是只读的,此行完成后,数组就消失了。
因此,您需要迭代数组的每个索引并更新每个值:
var members = sampleMembers()
for i in members.indices {
members[i].active = false
}
答案 1 :(得分:0)
示例:
struct ClubMembers {
var name: String
var active: Bool
}
func sampleMembers() -> [ClubMembers] {
let member1 = ClubMembers(name: "David", active: true)
let member2 = ClubMembers(name: "John", active: true)
let member3 = ClubMembers(name: "Mitchell", active: true)
return [member1, member2, member3]
}
var array = sampleMembers()
for i in 0..<array.count {
array[i].active = false
}
for i in 0..<array.count {
print(array[i].active)
}