Thursday 14 January 2016

How to declare Sets in Swift


declare sets.jpg
Hi Readers!
Let us first understand what Sets means.
Sets
“A set stores unique values of same type in a collection with no ordering. Sets should be used when there is no defined order, such as in Array. Secondly Array can store multiple same values but Sets can't. Sets are distinct."
Hash Values for Set Types
Whatever type you take it should be hashable to be stored in a set. It means that the type must provide a way to compute a hash value. A hash value is an Int value so that same objects can be compared equally, such as if `a == b`, if follows that `a.hashValue == b.hashValue`.
All basic swift types (Int, String, Bool and Double) are hashable by default. Enumerations case values are also hashable by default.
Set Type Syntax
Swift Set is written as `Set` where `Element` is the type that the set will store.
Creating and Initializing an Empty Set
  1. var pets = Set<String>()
Here pets is set which can store String values
Creating a Set with values The example below creates a set called pets to Store String values:
Here is not the end…. To view the full blog about How to declare Sets in Swift visit Findnerd.

Also visir Findnerd for such more tech Blogs like Android,Java, Javascript,C,C++,Ios Development Blogs etc.

No comments:

Post a Comment