Generic Subscripts

Welcome to one of the least favored lessons of all time. Hopefully, this lesson is nice and short since I don’t like using subscripts at all. Let’s get started.

Introduction

Problem

Can subscripts return T?

Your past

You remember in Chapter 1, this is probably how I introduced you to subscripts in Swift, whose role is to provide a shortcut.

  1. struct WeekDays {
  2. private var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
  3. subscript(index: Int) -> String {
  4. return days[index]
  5. }
  6. }

Well, we can make it a little bit more complex and potentially more scalable.

JSON

A json file looks like below.

  1. {
  2. "name": "Bob",
  3. "age": 21,
  4. "favoriteFood": ["Pizza", "Carbonara", "Hamburger"]
  5. }

Introducing Generic Subscripts

Let us create how to “design” a json like structure in Swift by using Dictionary.

  1. struct JSON {
  2. private var storage: [String: Any]
  3. init(dictionary: [String: Any]) {
  4. self.storage = dictionary
  5. }

Create JSON

  1. let republicOfKorea = JSON(dictionary: [
  2. "capital": "Seoul",
  3. "name": "Republic of Korea",
  4. "population": 50000000
  5. ])

Introducing Generic Subscripts

  1. struct JSON {
  2. private var storage: [String: Any]
  3. init(dictionary: [String: Any]) {
  4. self.storage = dictionary
  5. }
  6. subscript<T>(key: String) -> T? {
  7. return storage[key] as? T
  8. }
  9. }

Access Value

Let us use the generic subscript method above.

  1. let koreaCapital: String? = republicOfKorea["capital"]
  2. let koreaName: String? = republicOfKorea["name"]
  3. let koreaPopulation: Int? = republicOfKorea["population"]

Make sure you define T? from any explicitly as shown as koreaCapital: String?.

My Opinion

I don’t recommend using Subscripts even if it provides generics because there isn’t much context available and it takes much effort to search through.

Source Code

9003_generic_subscripts

Resources

Generic Subscripts - Apple

Conclusion

Just to recap, in the very first chapter, you’ve learned that Swift provides a shortcut for accessing methods and properties. However, I warned you because that shortcut comes along with a big tradeoff. First, it is hard to found and second, it does not provide context unless the name of an object does. Also, even if you use generic subscripts, I don’t like the fact that I have to explicitly define the placeholder type of T. It definitely can cause a huge issue for your teammates if they have not read or understand the object he/she is working with. So, I recommend not to use them. But, of course, if it is something quick and easy. feel free to.