say i have a very simple Person class
class Person { var name:String init(name:String) { self.name = name } } and i wish to store a collections of such Persons in a property, which is an array with type Person, of a People class
class People { var list:[Person] = [] } perhaps i achieve this as follows
var alex = Person(name:"Alex") var people = People() people.list.append(alex) QUESTION: how do i check if people.list contains the instance alex, please?
my simple attempt, which i was hoping to return true
people.list.contains(alex) calls an error "cannot convert value of type 'Person' to expected argument type '@noescape (Person) throws -> Bool'"
Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false.
For primitive values, use the array. includes() method to check if an array contains a value. For objects, use the isEqual() helper function to compare objects and array. some() method to check if the array contains the object.
The includes() method checks if a value is in an array. This method returns true or false depending on the outcome. The filter() method determines if an array of objects contains a particular value. This method returns the object that meets a certain criterion if it exists in the array.
Each variable or object in an array is called an element. Unlike stricter languages, such as Java, you can store a mixture of data types in a single array. For example, you could have array with the following four elements: an integer, a window object, a string and a button object.
There are two contains functions:
extension SequenceType where Generator.Element : Equatable { /// Return `true` iff `element` is in `self`. @warn_unused_result public func contains(element: Self.Generator.Element) -> Bool } extension SequenceType { /// Return `true` iff an element in `self` satisfies `predicate`. @warn_unused_result public func contains(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool } The compiler is complaining because the compiler knows that Person is not Equatable and thus contains needs to have a predicate but alex is not a predicate.
If the people in your array are Equatable (they aren't) then you could use:
person.list.contains(alex) Since they aren't equatable, you could use the second contains function with:
person.list.contains { $0.name == alex.name } or, as Martin R points out, based on 'identity' with:
person.list.contains { $0 === alex } or you could make Person be Equatable (based on either name or identity).
QUESTION: how do i check if people.list contains the instance alex, please?
class Person is a reference type, and var alex is a reference to the object storage. The identical-to operator === checks if two constants or variables refer to the same instance of a class.
Therefore, in order to check if the list contains a specific instance, use the predicate-based contains() method, and compare instances with ===:
if people.list.contains({ $0 === alex }) { // ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With