Learn Swift Fundamentals by preparing for Job Interview, Question 118 For Subscripts

Subscript is not a difficult concept at all, but apple doc explains it in an incomprehensible way .

109) What is subscript? How to declare subscript? Advantages?

Answer:

  • Subscript is a shortcut way of accessing Class or Structure’s collection.
  • Subscript is a feature that enables classes / structures’ to access their values in a shortcut way.
struct School {

var students: [String] = ["Paige", "Sunghee", "Seunghyun"]
subscript(index: Int) -> String {
return students[index]
}
}let school: School = School()
print(school.students[0]) // Paige
print(school[1]) // Sunghee

--

--

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!