Collection Types in Swift 4 - Arrays, Dictionaries, Sets. - Swift 4 Tutorials W3Schools

Hot

Post Top Ad

16 Aug 2017

Collection Types in Swift 4 - Arrays, Dictionaries, Sets.

In iOS Collection Types are used for storing data based on required data structure. Mainly there are three types.


1. Arrays
2. Dictionaries
3. Sets



Collection Types in Swift 4, Swift 3 , Objective - C,iOS




Arrays:

Array is an collection of objects in a sequential order. Array is the most important data structure for storing objects.

The objects are in sequential order and the key for accessing these objects is their index, an integer specifying a value’s position in the sequence. Index starts from 0 to n-1. Where n is number of objects in an array.

Objects may be Int, Double, String etc..

Example:

var animals = ["Lion", "Tiger", "Elephant"] // Array with Strings As Objects

var userIds = [12, 16, 22] // Array with Ints As Objects


Array, NSArray, NSMutableArray, Lists in Swift 4, Swift 3, Objective - C, iOS


For more examples and detailed explanation on Arrays.

Dictionaries:

Dictionary is unordered collection of objects with a key value pair.

For accessing an object we use keys. Dictionaries also allow for fast insertion and deletion operations.

For example we create dictionary for elephant.

Example:

var dict = [
    "name"  : "Elephant",
    "color" : "Black",
    "gender": "Male",
    "Weight": "5500 Kgs"
]


Dictionaries, NSDictionary, NSMutableDictionary HashMaps in Swift 4, Swift 3, Objective - C, iOS

For more examples and detailed explanation on Dictionaries .

Sets:

Set is an unordered collection of unique elements. Sets are like same as Arrays. When we don't need any order and ensure that each element appears only once in a collection.

The main advantage of sets is that we can perform so many operation that array doesn't. We can perform union, intersection of array using sets.

Example:

var animals: Set = ["Lion", "Tiger", "Elephant"] // same as array but 'Set' type


Sets, Union, Intersection in Swift 4, Swift 3, Objective - C, iOS

No comments:

Post a Comment

Post Top Ad