Skip to content

Latest commit

 

History

History

section20-collections

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Collections 1 ~ 4

1) What are the collections available in Swift?

Answer:

  • String
  • Array
  • Dictionary
  • Set
/** All Collections are interable **/

let helloworld: String = "hello world"
for value in helloworld {
	print(value)
}

let arr: [Int] = [1, 2, 3, 4, 5]
for value in arr {
	print(value)
}

let setData: Set<Int> = [1, 2, 3, 4, 5]
for value in setData {
	print(value)
}

let dic: [String: Any] = [
	"key": "value",
	"key2": "value2"
]

for (key, value) in dic {
	print(key)
	print(value)
}

2) What is the difference between Array and Set?

Answer:

  • Array is an ordered collection of similar or dissimilar values where Set is an unordered collection of similar values without duplicate elements.
  • Array is an ordered collection
  • Set is an unordered collection
  • Set contains unique values
let arr: Array = [1, 1, 2, 2, 3, 3]
for i in arr {
    print(i) // 1, 1, 2, 2, 3, 3
}

let aSet: Set = [1, 1, 2, 2, 3, 3]
for i in aSet {
    print(i)
    // It's an unordered collection
    // all posibilities..
    // 2, 3, 1
    // 1, 2, 3
    // 3, 2, 1
    // 1, 3, 2
}

3) What is the difference between Tuple vs Dictionary?

Answer:

  • Tuples are mainly used to return multiple values from a method.
  • Dictionaries are mainly to store the data in the form of key value pairs.
  • Tuples can optionally have keys.
  • Dictionary contains keys.
  • Tuple contains pre-defined number of values.
  • Dictionary has no limitation on storing key-value pairs.
  • Tuple has no methods to perform operations.
  • Dictionary has set of methods to perform operations.

4) Is String, Array, Dictionary and Set are Classes or Structures?

Answer:

  • String, Array, Dictionary and Set are Structures. Those are value types.

Table Of Contents

Section 1, Data Type

Section 2, Operator

Section 3, Conditional Statement

Section 4, Enum

Section 5, functions

Section 6, struct

Section 7, initializers

Section 8, closures

Section 9, OOP

Section 10, static type vs dynamic type

Section 11, optional

Section 12, generic

Section 13, subscript

Section 14, access specifier

Section 15, higher order function

Section 16, delegate

Section 17, extension

Section 18, Memory Management

Section 19, protocols

Section 20, collections

Section 21, KVO and KVC

Section 22, Exception Handling

Section 23, Framework

Section 24, Objective-C