Reputation: 635
I was wondering if there is an easy way to set a specific var in an array of struct elements?
Example: What I have is:
var memberArray = [Member]()
struct Member {
var memberID : String!
var memberName : String!
init(memberID : String! = nil,
memberName : String! = nil) {
self.memberID = memberID
self.memberName = memberName
}
}
So, how would I set e.g. the memberName
of a specific Member by just knowing it's memberID
?
Thanks for helping! :-)
Upvotes: 0
Views: 30
Reputation: 154583
Well, you have to search for the index of the record you want, and then use that index to modify the specified field of that record:
if let index = arr.index(where: { $0.memberId == "123" })
{
arr[index].memberName = "Fred"
}
Upvotes: 2