In iOS Collection Types are used for storing data based on required data structure. Mainly there are three types.
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..
For accessing an object we use keys. Dictionaries also allow for fast insertion and deletion operations.
For example we create dictionary for elephant.
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.
1. Arrays
2. Dictionaries
3. Sets
2. Dictionaries
3. Sets
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
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"
]
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
No comments:
Post a Comment