Find an Item and Change Value in Custom Object Array - Swift

Find an item and change value in custom object array - Swift

Since you are using a class, use filter and first to find the value:

array.filter({$0.eventID == id}).first?.added = value

In this you:

  1. filter the array down to elements that match the event ID
  2. pick the first result, if any
  3. then set the value

This works since classes are pass by reference. When you edit the return value from array.filter({$0.eventID == id}).first?, you edit the underlying value. You'll need to see the answers below if you are using a struct

EDIT: In Swift 3 you can save yourself a couple of characters

array.first({$0.eventID == id})?.added = value

EDIT: Swift 4.2:

array.first(where: { $0.eventID == id })?.added = value
array.filter {$0.eventID == id}.first?.added = value

Update an object in an array

Okay, this works for me.

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

Update/change array value of custom object- swift

With struct

for i in displayListCourseFilter.indices {
for j in displayListCourseFilter[i].content.indices {
var item = displayListCourseFilter[i].content[j]
if pId == item.parentID {
item.selectAll = "true"
item.isSelected = "true"
displayListCourseFilter[i].content[j] = item
}
}
}

or make it a class and it will compile

How can I find a specific item from custom object in a huge array - Swift

If suppose the ViewModelCourseTypeFilter instance is like,

let viewModel = ViewModelCourseTypeFilter(displayedCourseTypeFilter: [
DisplayedCourseTypeFilter(titlesCourseType: "First", isSelectedType: true),
DisplayedCourseTypeFilter(titlesCourseType: "Second", isSelectedType: false),
DisplayedCourseTypeFilter(titlesCourseType: "Third", isSelectedType: true)
])

Then you can get all titlesCourseType values where isSelectedType = true like so,

let arr = viewModel.displayedCourseTypeFilter.compactMap({ $0.isSelectedType ? $0.titlesCourseType : nil })
print(arr) //["First", "Third"]

Update element in array

Do not use filter(), it doesn't make sense.

The point of filter is to iterate each elements and tells if you keep it (return true) or not (return false) according to your desires.

Instead, use map():

numberWords = numberWords.map({ return $0 == "one" ? "1" : $0 })

I used a ternary if and explicitly write a "return" (which is not necessary since it's already done "internally" (you need to returns something)), but you can do it more explicitly keeping your previous code:

numberWords = numberWords.map({
if $0 == "one" {
return "1"
}
return $0 })

map() is more suited for that. You use it if you want to iterate each items and modify it if needed which is what you want to do.

How would I change the value of a specific element in an array?

you can use this code safely with no issue for that func:



func changeValueFor(index: Int, to newName: String) {

if myArray.indices.contains(index) {
myArray[index] = newName
}
else {
print("Error! there is no such index found!")
}

}

How to update a specific property value of all elements in array using Swift

You just need to iterate your array indices using forEach method and use the array index to update its element property:

struct ViewHolder {
let name: String
let age: Int
var isMarried: Bool
}

var viewHolders: [ViewHolder] = [.init(name: "Steve Jobs", age: 56, isMarried: true),
.init(name: "Tim Cook", age: 59, isMarried: true)]

viewHolders.indices.forEach {
viewHolders[$0].isMarried = false
}

viewHolders // [{name "Steve Jobs", age 56, isMarried false}, {name "Tim Cook", age 59, isMarried false}]

You can also extend MutableCollection and create a mutating method to map a specific property of your collection elements as follow:

extension MutableCollection {
mutating func mapProperty<T>(_ keyPath: WritableKeyPath<Element, T>, _ value: T) {
indices.forEach { self[$0][keyPath: keyPath] = value }
}
}

Usage:

viewHolders.mapProperty(\.isMarried, false)
viewHolders // [{name "Steve Jobs", age 56, isMarried false}, {name "Tim Cook", age 59, isMarried false}]


Related Topics



Leave a reply



Submit