nsstring - .containsString in Swift 2? -
previously, when wanted see if swift string contained string, cast nsstring , call .containsstring. apple, in infinite wisdom, made version-aware, if try under s2 demand #available wrapper if target platform support (which guess bug).
so best solution appears this:
extension string { func contains(substr: string) -> bool { if #available(osx 10.10, *) { return nsstring(string: self).containsstring(substr) } else { return self.rangeofstring(substr) != nil } } }
and check it, instead of this:
if nsstring(string: line).containsstring(" ")...
you use nicer looking:
if line.contains(" ")...
this no longer complains version, , (imho) looks better too. want too:
extension string { var length: int { return self.characters.count } }
apple keeps changing way length, , hope future changes api #available
-able, @ point .length
can modified. , these sanity:
extension string { subscript (r: range<int>) -> string { { let substart = advance(self.startindex, r.startindex, self.endindex) let subend = advance(substart, r.endindex - r.startindex, self.endindex) return self.substringwithrange(range(start: substart, end: subend)) } } func substring(from: int) -> string { let end = self.characters.count return self[from..<end] } func substring(from: int, length: int) -> string { let end = + length return self[from..<end] } } extension string { func trim() -> string { return self.stringbytrimmingcharactersinset(nscharacterset.whitespaceandnewlinecharacterset()) } func trim(withset: nscharacterset) -> string { return self.stringbytrimmingcharactersinset(withset) } }
Comments
Post a Comment