Last active
March 14, 2018 19:25
-
-
Save fethica/3791a7008da9451c80b1e1c7441340f2 to your computer and use it in GitHub Desktop.
Example of subscript in Swift
This file contains hidden or 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
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