[iOS/Swift] Subscript를 이용하여 배열에 안전하게 접근하는법 (Index out of range X!!!!!!)

김혜수·2023년 5월 16일
0

iOS를 개발하면서..

목록 보기
20/20
extension Collection {
    subscript (safe index: Index) -> Element? {
        return indices.contains(index) ? self[index] : nil
    }
}

다음 extension을 사용하고 아래와 같이 쓰면 된다

self.frequencyTags[safe: row]
profile
iOS를 좋아하는 사람

0개의 댓글