在自定义对象数组中查找项目并更改值 - Swift

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


I have this class

我有这门课

class InboxInterests {

    var title = ""
    var eventID = 0
    var count = ""
    var added = 0

    init(title : String, eventID : NSInteger, count: String, added : NSInteger) {
        self.title = title
        self.eventID = eventID
        self.count = count
        self.added = added

    }
}

And i use it like this

我就是这样用的

var array: [InboxInterests] = [InboxInterests]()

Add item

新增项目

let post = InboxInterests(title: "test",eventID : 1, count: "test", added: 0)
self.array.append(post)

I want to find the index by eventID key and change the value of added key in the same index

我想通过eventID键找到索引,并在同一索引中更改已添加键的值

How is that possible?

怎么可能?

3 个解决方案

#1


16  

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. 将数组过滤到与事件ID匹配的元素
  3. pick the first result, if any
  4. 选择第一个结果,如果有的话
  5. then set the value
  6. 然后设置值

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

编辑:在Swift 3中,你可以节省几个字符

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

#2


12  

For me, the above answer did not work. So, what I did was first find the index of the object that I want to replace then using the index replace it with the new value

对我来说,上面的答案没有用。所以,我所做的是首先找到我要替换的对象的索引,然后使用索引将其替换为新值

if let row = self.upcoming.index(where: {$0.eventID == id}) {
       array[row] = newValue
}

#3


5  

The filter operator is not the best in this case, it works for some of you because classes are passed by reference.

在这种情况下,过滤器运算符不是最好的,它适用于某些人,因为类是通过引用传递的。

Explanation: (You can copy the following code in a playground if you want to verify it).

说明:(如果要验证,可以在操场中复制以下代码)。

class Book {
    let id: Int
    var title = "default"

    init (id: Int) {
        self.id = id
    }
}
var arrayBook = [Book]()
arrayBook.append(Book(id: 0))
arrayBook.append(Book(id:1))
arrayBook.forEach { book in
    print(book.title)
}

arrayBook.filter{ $0.id == 1 }.first?.title = "modified"

arrayBook.forEach { book in
    print(book.title)
}

Arrays are copied by value not reference, so when you are using filter you are creating a new array (different than the initial), but when you modify the new one, the initial one gets modified too because both are pointing to the same class (classed are passed by reference), so after the filter your array will have changed and the new one gets deallocated. So in this case it will print "default", "default" and then "default, "modified".

数组是通过值而不是引用复制的,因此当您使用过滤器时,您正在创建一个新数组(与初始数组不同),但是当您修改新数组时,初始数组也会被修改,因为它们都指向同一个类( (通过引用传递分类),因此在过滤器之后,您的数组将更改并且新的数组将被取消分配。因此,在这种情况下,它将打印“默认”,“默认”,然后“默认”,“修改”。

What happens if you change class for struct, the value will be passed by value not reference so you will have 2 arrays in memory with different values, so if you go through arrayBooks again it will print before the filter "default","default", and then "default", "default" again. Because when you are using the filter you are creating and modifying a new array that will get deallocated if you do not store it).

如果更改struct的类会发生什么,该值将通过值而不是引用传递,因此您将在内存中有2个不同值的数组,因此如果再次通过arrayBooks,它将在过滤器“default”,“default”之前打印,然后再“默认”,“默认”。因为当您使用过滤器时,您正在创建和修改一个新阵列,如果您不存储它将被解除分配)。

The solution is using map, creating a new array with all the values but with the modified items or fields that we want and then replace our array with the new one. This will print "default", "default" before the map, and then "default", "modified"

解决方案是使用map,创建一个包含所有值的新数组,但是使用我们想要的修改的项目或字段,然后用新的数组替换我们的数组。这将在地图前打印“默认”,“默认”,然后“默认”,“修改”

This will work with structs, classes and everything that you want :).

这将适用于结构,类和您想要的所有内容:)。

struct Book {
    let id: Int
    var title = "default"

    init (id: Int) {
        self.id = id
    }
}
var arrayBook = [Book]()
arrayBook.append(Book(id: 0))
arrayBook.append(Book(id:1))
arrayBook.forEach { book in
    print(book.title)
}

arrayBook = arrayBook.map{
    var mutableBook = $0
    if $0.id == 1 {
        mutableBook.title = "modified"
    }
    return mutableBook
}

arrayBook.forEach { book in
    print(book.title)
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2016/06/28/4490651e050de9c2ebdbecbb482a78b8.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号