You cannot just enumeratate by strings over a string. you need to tell what ranges the substring has to have.
Here it is done by words:
var str:String = "Hello World!"
str.enumerateSubstringsInRange(Range<String.Index>(start:str.startIndex , end: str.endIndex), options: NSStringEnumerationOptions.ByWords) { (substring, substringRange, enclosingRange, stop) -> () in
switch substring
{
case "Hello":
println("Hi")
case "World":
println("universe")
default:
break
}
}
but actually I am cheating. In you code you want to switch for World!
, but I am using World
. I do this as in word-based enumeration non-alphanumeric characters are ignored.
But we have all information to fix it
var str:String = "Hello World!"
str.enumerateSubstringsInRange(Range<String.Index>(start:str.startIndex , end: str.endIndex), options: NSStringEnumerationOptions.ByWords) { (substring, substringRange, enclosingRange, stop) -> () in
var enclosingStr = str.substringWithRange(enclosingRange)
enclosingStr = enclosingStr.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
switch enclosingStr
{
case "Hello":
println("Hi")
case "World!":
println("universe")
default:
break
}
}