Day 4 : Collections in Swift

  1. Collections in Swift: Swift can handle collection of all datatypes in arrays, dictionary and sets. Arrays in Swift can hold only one type of datatypes, that means if an array is declared for string then it can only store strings.

var array = []

var abc: String = [“abc”, “xyz”]

2. Adding Items to Array: We cannot add items to immutable(constant) arrays. For mutable(variable) arrays we can use either use .append method or concatenation.

//add item to end of array using append method

todo.append(“this was appended”)

//concatenating two arrays

[1, 2, 3]+[4]

todo += [“this was concatenated”]

//Immutable Arrays

let array = [“whatsapp”]

//array.append(“facebook”) Error !

//array+=[“facebook”] Error !

3. Inserting and Deleting Values in Arrays: Its really simple to insert and delete values in arrays.

//insert using index value

todo.insert(“Watch TV”, at: 2)

//removing items from array

todo.remove(at: 2)

4. Length of array: .count method gives l+1 value when called on an array i.e. an array having total 7 items when called with .count method will give a value of 8 rather than 7.

var todo: [Int] = [1, 2, 3, 4]

todo.count //value = 5

SHORT CODE-SUMMARY OF WHAT I LEARNED TODAY: (last modified: 08/05/2017)[please update if u change anything!]

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.