-
-
Save sturdysturge/5f2e14f547ce1f1d7a2b47ed8a76650b to your computer and use it in GitHub Desktop.
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
import Foundation | |
extension Array { | |
subscript(index: Index, default defaultValue: @autoclosure () -> Element) -> Element { | |
if indices.contains(index) { | |
//If the index is valid we have no problem returning the value | |
return self[index] | |
} else { | |
//Otherwise we return the default which can't be nil | |
return defaultValue() | |
} | |
} | |
subscript(index: Index, default defaultValue: @autoclosure () -> Element?) -> Element? { | |
if indices.contains(index) { | |
//If the index is valid we have no problem returning the value | |
return self[index] | |
} else { | |
//Otherwise we return the default which may be nil | |
return defaultValue() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment