Swift

  1. Structs vs. Classes

    Try to use structs over classes when possible. They are safer in a multi-thread environment, faster, they have a default init method and many other benefits. But understand the difference and keep in mind that structs are a value type while classes are a reference type, meaning that each instance of a struct has its own unique copy of it, while each instance of a class has a reference to one single copy of the data.

  2. Subscripts

     extension String {
         subscript(i: Int) -> String {
             return String(self[index(startIndex, offsetBy: i)])
         }
     }
    

    Usage:

     let str = "Hello, Playground!" 
     print("Letter at index 7 is \(str[7])")
    

    Output:

     Letter at index 7 is P
    

References

  1. Medium
  2. hackingwithswift