How to iterate through array of objects in Swift?

后端 未结 6 812
慢半拍i
慢半拍i 2021-02-01 02:24

i have objects

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

var person2 = Person()
person2.name = \"Julia\"
person2         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 03:07

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

提交回复
热议问题