Skip to content

Instantly share code, notes, and snippets.

@fethica
Last active March 14, 2018 19:25
Show Gist options
  • Save fethica/3791a7008da9451c80b1e1c7441340f2 to your computer and use it in GitHub Desktop.
Save fethica/3791a7008da9451c80b1e1c7441340f2 to your computer and use it in GitHub Desktop.
Example of subscript in Swift
struct daysofaweek {
private var days = [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “saturday”]
subscript(index: Int) -> String {
get {
return days[index]
}
set(newValue) {
days[index] = newValue
}
}
}
var p = daysofaweek()
print(p[0]) // prints sunday
p[0] = “Monday”
print(p[0]) // prints Monday
// Source: https://medium.com/@abhimuralidharan/subscripts-in-swift-51e73cc5ddb5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment