Last active
April 8, 2019 13:46
-
-
Save Zeta611/9b8108edc2955114046408b10c11b268 to your computer and use it in GitHub Desktop.
[Collection+subscript] Returns the element at the index if it is within range, otherwise `nil` #extension #iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Collection+subscript.swift | |
// | |
// Created by Jay Lee on 08/04/2019. | |
// Copyright © 2019 Jay Lee <jaeho.lee@snu.ac.kr> | |
// This work is free. You can redistribute it and/or modify it under the | |
// terms of the Do What The Fuck You Want To Public License, Version 2, | |
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details. | |
// | |
import Foundation | |
extension Collection { | |
/// Returns the element at the index if it is within range, otherwise `nil` | |
subscript (safe index: Index) -> Element? { | |
return indices.contains(index) ? self[index] : nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment