更新数组中项目的最简单/最直接的方法是什么?我希望调用者也有更新的数组。所以:
static func updateItem(updatedItem: Item, inout items: [Item]) -> Bool {
var item = items.filter{ $0.id == updatedItem.id }.first
if item != nil {
item = updatedItem
return true
}
return false
}
我希望调用者拥有更新的项目(使用更新的项目)。我认为上面代码的问题是它只更新局部变量项。实际更新items数组中相关项的最佳方法是什么?
你这样做的方式就像超人穿上他的裤袜一样 - 一次一条腿。循环通过传入的inout
数组并替换id
匹配的任何项目:
func updateItem(updatedItem: Item, items: inout [Item]) -> Bool {
var result = false
for ix in items.indices {
if items[ix].id == updatedItem.id {
items[ix] = updatedItem
result = true
}
}
return result
}
请注意,这是Swift 3语法,其中inout
在类型之前,而不是标签。
你可以使用map
将它写得更加“Swiftily”:
func updateItem(updatedItem: Item, items: inout [Item]) {
items = items.map {
$0.id == updatedItem.id ? updatedItem : $0
}
}
......但最终这相当于同样的事情。
你正在改变item
,它只是数组中实例的副本(如果Item
是一个值类型,如struct
,tuple
或enum
),或者它的引用(如果Item
是引用类型,例如`类)。在任何一种情况下,阵列都不会受到影响。
您需要在数组中找到实例的索引,然后在该索引处改变数组。
func updateItem(updatedItem: Item, inout items: [Item]) -> Bool {
guard let index = items.index(where: { $0.id == updatedItem.id }) else {
return false // No matching item found
}
items[index] = updatedItem
return true
}
不过,这一切都相当笨重。如果您使用字典而不是将id
映射到具有该id
的实例,那会更好。这意味着您将拥有快速,恒定的时间查找,并且它将更方便。这是看起来如何:
// Assuming the "id" is an Int
func updateItem(updatedItem: Item, items: inout [Int: Item]) -> Bool {
return items.updateValue(updatedItem, forKey: updatedItem.id) != nil
}