Check out example codes for "swift out of bound elelemnt". It will help you in understanding the concepts better.

Code Example 1

extension Collection {
    subscript(safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

Learn ReactJs, React Native from akashmittal.com