如何在Swift中迭代对象数组?

时间:2023-01-23 17:11:00

i have objects

我有对象

var person1 = Person()
person1.name = "Joe"
person1.lastName = "Doe"
person1.age = 21

var person2 = Person()
person2.name = "Julia"
person2.lastName = "Ivanova"
person2.age = 22

var person3 = Person()
person3.name = "Irina"
person3.lastName = "Petrova"
person3.age = 25

var person9 = Person()
person9.name = "Vova"
person9.lastName = "Vovin"
person9.age = 32

var person10 = Person()
person10.name = "Masha"
person10.lastName = "Golovanova"
person10.age = 20

var person11 = Person()
person11.name = "Petra"
person11.lastName = "Andreeva"
person11.age = 27

and multi array

和多阵列

var array = [[person1, person2, person3], [person9, person10, person11]]

how can I iterate through array to get for example a person with name="Masha"

如何迭代数组以获得名称=“Masha”的人

thanks in advance

提前致谢

5 个解决方案

#1


47  

I would try this:

我会试试这个:

var array:[[Person]] = [[person1, person2, person3], [person9, person10, person11]]
/*Casting it like this should keep from getting an error later 
   and/or having to recast the objects*/

for people in array {

/*This is going to look at each array in arrays, 
   and call each one 'people' within this loop*/

    for person in people {

    /*Same thing, this is going to look at each item in the people array
       and call each one 'person' within this loop*/

        if person.name == "Masha" {
            return person
        }
    }
}

#2


18  

An interesting way of solving this would be to leverage the flatMap function of Swift.

解决这个问题的一个有趣方法是利用Swift的flatMap函数。

var array = [[person1, person2, person3], [person9, person10, person11]]

let flatArray = array.flatMap { $0 }

flatArray now is [person1, person2, person3, person9, person10, person11] of type Array<Person>.

flatArray现在是Array 类型的[person1,person2,person3,person9,person10,person11]。

Then you can iterate on flatArray to find what you want :

然后你可以迭代flatArray来找到你想要的东西:

for person in flatArray {
    if person.name == "Masha" {
        // Do something
    }
}

You could even go further by using Swift 2.0 because it allows you to put a where clause in the for :

您甚至可以通过使用Swift 2.0进一步,因为它允许您在for中放置where子句:

for person in flatArray where person.name == "Masha" {
    // Do something
}

#3


14  

Here is technique which called 'Chaining'

这是称为'链接'的技术

Playground. Swift 3.1

操场。 Swift 3.1

import UIKit

class Person {
    var name = ""
    var lastName = ""
    var age = 0
}

var person1 = Person()
person1.name = "Joe"
person1.lastName = "Doe"
person1.age = 21

var person2 = Person()
person2.name = "Julia"
person2.lastName = "Ivanova"
person2.age = 22

var person3 = Person()
person3.name = "Irina"
person3.lastName = "Petrova"
person3.age = 25

var person9 = Person()
person9.name = "Vova"
person9.lastName = "Vovin"
person9.age = 32

var person10 = Person()
person10.name = "Masha"
person10.lastName = "Golovanova"
person10.age = 20

var person11 = Person()
person11.name = "Petra"
person11.lastName = "Andreeva"
person11.age = 27

var array = [[person1, person2, person3], [person9, person10, person11]]

array
    .flatMap{$0}
    .forEach { (anyPerson:Person) in

        if anyPerson.name == "Masha" {
            //Do what you need
            print(anyPerson.lastName)
        }
}

Screen-shot from playground: 如何在Swift中迭代对象数组?

从操场上截屏:

#4


9  

Iterating can be done as in @Rachel's answer. However there are different ways of doing the same thing, and not necessarily by iterating - and in some cases with just one line of code.

迭代可以像在@ Rachel的答案中那样完成。然而,有不同的方法来做同样的事情,而不一定通过迭代 - 在某些情况下只有一行代码。

If you want to find, then the best way is using the array's filter method - but before that the multi dimensional array should be flattened, using flatMap.

如果你想找到,那么最好的方法是使用数组的滤镜方法 - 但在此之前,应使用flatMap将多维数组展平。

So, to find the first element having a certain name, you can simply do:

因此,要找到具有特定名称的第一个元素,您可以简单地执行:

let result: Person? = array.flatMap { $0 }.filter { $0.name == "Masha" }.first

#5


4  

swift 3 :

 forEach({element in
   // do what you need
 })

#1


47  

I would try this:

我会试试这个:

var array:[[Person]] = [[person1, person2, person3], [person9, person10, person11]]
/*Casting it like this should keep from getting an error later 
   and/or having to recast the objects*/

for people in array {

/*This is going to look at each array in arrays, 
   and call each one 'people' within this loop*/

    for person in people {

    /*Same thing, this is going to look at each item in the people array
       and call each one 'person' within this loop*/

        if person.name == "Masha" {
            return person
        }
    }
}

#2


18  

An interesting way of solving this would be to leverage the flatMap function of Swift.

解决这个问题的一个有趣方法是利用Swift的flatMap函数。

var array = [[person1, person2, person3], [person9, person10, person11]]

let flatArray = array.flatMap { $0 }

flatArray now is [person1, person2, person3, person9, person10, person11] of type Array<Person>.

flatArray现在是Array 类型的[person1,person2,person3,person9,person10,person11]。

Then you can iterate on flatArray to find what you want :

然后你可以迭代flatArray来找到你想要的东西:

for person in flatArray {
    if person.name == "Masha" {
        // Do something
    }
}

You could even go further by using Swift 2.0 because it allows you to put a where clause in the for :

您甚至可以通过使用Swift 2.0进一步,因为它允许您在for中放置where子句:

for person in flatArray where person.name == "Masha" {
    // Do something
}

#3


14  

Here is technique which called 'Chaining'

这是称为'链接'的技术

Playground. Swift 3.1

操场。 Swift 3.1

import UIKit

class Person {
    var name = ""
    var lastName = ""
    var age = 0
}

var person1 = Person()
person1.name = "Joe"
person1.lastName = "Doe"
person1.age = 21

var person2 = Person()
person2.name = "Julia"
person2.lastName = "Ivanova"
person2.age = 22

var person3 = Person()
person3.name = "Irina"
person3.lastName = "Petrova"
person3.age = 25

var person9 = Person()
person9.name = "Vova"
person9.lastName = "Vovin"
person9.age = 32

var person10 = Person()
person10.name = "Masha"
person10.lastName = "Golovanova"
person10.age = 20

var person11 = Person()
person11.name = "Petra"
person11.lastName = "Andreeva"
person11.age = 27

var array = [[person1, person2, person3], [person9, person10, person11]]

array
    .flatMap{$0}
    .forEach { (anyPerson:Person) in

        if anyPerson.name == "Masha" {
            //Do what you need
            print(anyPerson.lastName)
        }
}

Screen-shot from playground: 如何在Swift中迭代对象数组?

从操场上截屏:

#4


9  

Iterating can be done as in @Rachel's answer. However there are different ways of doing the same thing, and not necessarily by iterating - and in some cases with just one line of code.

迭代可以像在@ Rachel的答案中那样完成。然而,有不同的方法来做同样的事情,而不一定通过迭代 - 在某些情况下只有一行代码。

If you want to find, then the best way is using the array's filter method - but before that the multi dimensional array should be flattened, using flatMap.

如果你想找到,那么最好的方法是使用数组的滤镜方法 - 但在此之前,应使用flatMap将多维数组展平。

So, to find the first element having a certain name, you can simply do:

因此,要找到具有特定名称的第一个元素,您可以简单地执行:

let result: Person? = array.flatMap { $0 }.filter { $0.name == "Masha" }.first

#5


4  

swift 3 :

 forEach({element in
   // do what you need
 })