Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Last active August 29, 2015 14:18
Show Gist options
  • Save joanmolinas/79e30af02e25b490f5c6 to your computer and use it in GitHub Desktop.
Save joanmolinas/79e30af02e25b490f5c6 to your computer and use it in GitHub Desktop.
extension String {
subscript(index : Int) -> Character{
get {
return self[advance(self.startIndex, index)]
} set(newValue) {
self = self.stringByReplacingCharactersInRange(Range<String.Index>(start: advance(startIndex, index), end: advance(startIndex, index+1)), withString: String(newValue))
}
}
}
var string = "Hello World!"
string[4] //OUTPUT -> "o"
string[4] = "y" //OUTPUT -> "Helly World!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment