Learn Swift Fundamentals by preparing for Job Interview, Questions 165 ~ 168 For Collections

If you’ve ever studied any other programming language, you’re probably familiar with collection. It’s almost at the level of the developer’s common sense. But what if you don’t know common sense?

165) 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)
}

166) 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 possibilities..
// 2, 3, 1
// 1, 2, 3
// 3, 2, 1
// 1, 3, 2
}

167) 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.

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

Answer:

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!