Swift: Consider adding get() to Arrays (or CollectionTypes)

Originator:owensd
Number:rdar://20234225 Date Originated:19-Mar-2015 07:10 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-beta (6D543q)
Classification:Enhancement Reproducible:Always
 
I find myself needing to add an extension to Arrays often in my projects that provides a safe indexer.

Something like:

extension Array {
    func get(index: Int) -> T? {
        if 0 <= index && index < self.count {
            return self[index]
        }
        
        return nil
    }
}

Then I can use it in the more natural approach:

if let item = items.get(someIndex) {
  // …
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!