2

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?

1

4 Answers 4

1

you can use:

var str = "Hello, playground"

if str.rangeOfString("Hello") != nil {
    println("exists")
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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

1 Comment

Thanks the example 2 is the best way i think :)!
0

In Swift is it called rangeOfString

"hello".rangeOfString("ell")

returns a range

{Some "1..<4"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.