Using the set type: each item must be unique. The items are not ordered. The type stored in the set must be hashable. A basic type (like string, int, double, float) are all hashable. They are able to compute hash values for themselves. To create an initialise a set: var mySet = Set<String>() //initialise values var mySet = Set(["ABC", "DEF", "GHI"]) //instead of var, you can use let Insert items into a set. If we try to insert something that is already there, it will be ignored. mySet.insert("JKL") To check to see if it has been inserted successfully: var results = mySet.insert("JKL") if results.inserted{} Get the number of items in a set: mySet.count Check to see if a set contains an item: var contain = mySet.contains("ABC") //will return true Iterating over each item in a set: for item in mySet { } Removing items in a set: var item = mySet.remove("ABC") mySet.removeAll() Construct sets from 2 sets: var set1 =...
Comments
Post a Comment