Wednesday, May 18, 2016

Filtering Arrays

The main thing to remember is that "$0" represents each item in the array.
let array = ["Mark", "Matt", "Matthew", "Bob"]
let filtered = array.filter({$0.containsString("Matt")})
print(filtered.count)


class Person {
    var name: String!
    var age: Int!
    
    init(_ name: String, _ age: Int) {
        self.name = name
        self.age = age
    }
}

var people = [Person]()
people.append(Person("Mark", 45))
people.append(Person("Matt", 46))
people.append(Person("Julie", 50))
people.append(Person("Patty", 51))
people.append(Person("Bob", 52))

let filteredPeople = people.filter({$0.age >= 50})
print(filteredPeople.count)

(Swift 2.2)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...