3

For example i have usual fast enumeration

for (var myChar:Character) in "Hello World!"
{//code }

This works fine and i can do whatever i want with each character of this string. But what if i want to use string instead of character, like this

for ( var myStr : String) in "Hello World!"//this is error
{
    switch myStr
    {
    case "Hello":
    //code
    case "World!":
    //code
    default:
        break
    }
}

Is it possible to implement this? Thanks

AnderCover
  • 2,488
  • 3
  • 23
  • 42
Egor
  • 41
  • 1
  • 3

3 Answers3

2

You can use componentsSeparatedByCharactersInSet to divide the string into an array of strings:

import Foundation // not necessary if you import UIKit or Cocoa

for myStr in "Hello World!".componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString:" ")) {
    switch myStr {
    case "Hello":
        println("well hello there")
    case "World!":
        println("the whole world")
    default:
        println("something else")
    }
}

In place of NSCharacterSet(charactersInString:" ") can also use NSCharacterSet.whitespaceCharacterSet() or NSCharacterSet.whitespaceAndnewlineCharacterSet() depending on what kinds of characters divide your words.


If your words are separated by a single space, you can use the shorter:

for myStr in "Hello World!".componentsSeparatedByString(" ") {
    ...
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • Did that work for you? If one of the answers solved your problem, please accept it by clicking on the checkmark next to the answer to turn it green. – vacawama Nov 29 '14 at 04:37
1

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
    }
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
1

While in Swift 2.0, I use

let line = "Hello, world"
for (index, value) in line.characters.enumerate() {
  // do something
}
hippo_san
  • 360
  • 2
  • 12