Skip to content

Instantly share code, notes, and snippets.

@rafaelks
Last active August 29, 2015 14:14
Show Gist options
  • Save rafaelks/8e928dda8e0b07525847 to your computer and use it in GitHub Desktop.
Save rafaelks/8e928dda8e0b07525847 to your computer and use it in GitHub Desktop.
Swift: NSRange vs Range <String.Index>
func exportStrings() -> Void {
var foo: String = ""
var bar: String = ""
if let result = result {
let rangeFoo = result.rangeOfString("foo:")
let rangeBar = result.rangeOfString("?bar=")
if let rangeFoo = rangeFoo {
if let rangeBar = rangeBar {
let indexStartAddress = rangeFoo.endIndex
let indexEndAddress = rangeBar.startIndex
let indexStartAmount = rangeBar.endIndex
let indexEndAmount = result.endIndex
foo = result[Range<String.Index>(start: indexStartAddress, end: indexEndAddress)]
bar = result[Range<String.Index>(start: indexStartAmount, end: indexEndAmount)]
} else {
foo = result[Range<String.Index>(start: rangeFoo.endIndex, end: result.endIndex)]
}
} else {
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment