I have a UITextView that is editable. I am having users type into this, and when they press the submit button, I want to be able to traverse through the string and check for certain words, like Apple, Bug, Dog, etc. If those words are present, I want the submit to cancel, and an alert appear. I just need to know how to check for the words, and then how to cancel the segue.
Asked
Active
Viewed 59 times
0
-
http://stackoverflow.com/questions/24034043/how-do-i-check-if-a-string-contains-another-string-in-swift. For the second part just setup a func that if you find any of the substrings you stop and display the alert otherwise you then call the segue. – Race B Jun 24 '15 at 17:10
1 Answers
1
This is easy to do with the rangeofString
method.
var string = "hello Swift"
if string.rangeOfString("Swift") != nil{
println("exists")
}
Note that this is case sensitive, so if you were looking for "swift" instead of "Swift" you would have to be explicit about that in your function call.
As far as cancelling the segue, the better practice would be to not initialize the segue until after you have done your string validation. This way you know it will only segue if the appropriate string has been entered.
Possible duplicate of question here: How do I check if a string contains another string in Swift?
-
Thank you! This worked perfectly, and I couldn't find that one when searching, so thank you for posting again. – Anthony Saltarelli Jun 24 '15 at 17:40