Filtering Swift Array of Objects by Property

The filter option allows you to filter through an array in Swift, returning only entries that match the filter specifications. The following example shows an array of Friend objects who have the attribute age. We create a filter that returns an array of friends whose age is less than 40.

var friends = [Friend(age: 50), Friend(age: 42), Friend(age: 22), Friend(age: 30)]
var filteredFriends = friends.filter({
    $0.age < 40
})
 
for friend in friends {
    println(friend.age!)
}