Before Beta 5 I could use
var str = "Hello, playground"
str.bridgeToObjectiveC().containsString("Hello")
But this is not longer supported, is there a nice workaround for this, or does Swift already offer it now?
Before Beta 5 I could use
var str = "Hello, playground"
str.bridgeToObjectiveC().containsString("Hello")
But this is not longer supported, is there a nice workaround for this, or does Swift already offer it now?
you can use:
var str = "Hello, playground"
if str.rangeOfString("Hello") != nil {
println("exists")
}
Just cast it explicitly to NSString
:
var str = "Hello, playground"
(str as NSString).containsString("Hello")
However if there's a pure swift way to do that, I would use it - it's always better to avoid bridging unless really needed.
In Swift is it called rangeOfString
"hello".rangeOfString("ell")
returns a range
{Some "1..<4"}
You can write extension contains:
extension String {
func contains(find: String) -> Bool{
if let temp = self.rangeOfString(find){
return true
}
return false
}
}
Example:
var value = "Hello world"
println(value.contains("Hello")) // true
println(value.contains("bo")) // false