swift3 - Value of "String" has no member 'indices' - moving to Swift 3.0 -
i'm moving swift 2 (probably, 2.3) 3.0 , have hard time correcting following code : now, know string not collection anymore. how correct particular code?
let separator = anyword.indexof("-")! anyword.substring(with: (anyword.indices.suffix(from: anyword.index(anyword.startindex, offsetby: separator + 1))))
as possible improvement @dfri's first solution, use upperbound
of string
's range(of:)
method (which bridged nsstring
) – give index after (first occurrence of the) dash in string.
import foundation let anyword = "foo-bar" if let separator = anyword.range(of: "-")?.upperbound { let bar = anyword.substring(from: separator) print(bar) // bar }
or compacted crazy single binding using optional
's map(_:)
method:
if let bar = (anyword.range(of: "-")?.upperbound).map(anyword.substring(from:)) { print(bar) // bar }
although note general solution problem of 'string not being collection anymore', use string.characters
instead, is collection
.
Comments
Post a Comment