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

时间:2015-07-27 11:53:51

标签: arrays swift for-loop multidimensional-array

我有对象

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]]

如何迭代数组以获取名称为&#34的人; Masha"

提前致谢

6 个答案:

答案 0 :(得分:57)

我会试试这个:

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
        }
    }
}

答案 1 :(得分:20)

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

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

let flatArray = array.flatMap { $0 }

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

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

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

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

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

答案 2 :(得分:17)

这是一种叫做#Chaining&#39;

的技术

游乐场 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)
        }
}

从操场上截屏: enter image description here

答案 3 :(得分:9)

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

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

因此,要找到具有特定名称的第一个元素,您只需执行以下操作:

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

答案 4 :(得分:7)

swift 3:

branch1a

答案 5 :(得分:1)

这是一种非常简单的方法来满足您的需求:

let result = array
    .flatMap { $0 }
    .first(where: { $0.name == "Masha" })

首先,必须将数组弄平,然后才可以从满足给定谓词的数组中获得第一个元素(在我们的例子中,name等于"Masha")。 / p>