Tutorial by Examples

Sets are unordered collections of unique values. Unique values must be of the same type. var colors = Set<String>() You can declare a set with values by using the array literal syntax. var favoriteColors: Set<String> = ["Red", "Blue", "Green", "Blu...
var favoriteColors: Set = ["Red", "Blue", "Green"] //favoriteColors = {"Blue", "Green", "Red"} You can use the insert(_:) method to add a new item into a set. favoriteColors.insert("Orange") //favoriteColors = {"Red&q...
var favoriteColors: Set = ["Red", "Blue", "Green"] //favoriteColors = {"Blue", "Green", "Red"} You can use the contains(_:) method to check whether a set contains a value. It will return true if the set contains that value. if favorite...
Common values from both sets: You can use the intersect(_:) method to create a new set containing all the values common to both sets. let favoriteColors: Set = ["Red", "Blue", "Green"] let newColors: Set = ["Purple", "Orange", "Green"] ...
In order to define a Set of your own type you need to conform your type to Hashable struct Starship: Hashable { let name: String var hashValue: Int { return name.hashValue } } func ==(left:Starship, right: Starship) -> Bool { return left.name == right.name } Now you can cr...
3.0 Swift 3 introduces the CountedSet class (it's the Swift version of the NSCountedSet Objective-C class). CountedSet, as suggested by the name, keeps track of how many times a value is present. let countedSet = CountedSet() countedSet.add(1) countedSet.add(1) countedSet.add(1) countedSet.a...

Page 1 of 1