user2632918
user2632918

Reputation:

String does not conform to protocol "intervalType"

I got the error

Type 'String' does not conform to protocol 'intervalType'

I tried this code only in a playgroundfile and it doesn't work also.

var header = [String:AnyObject]()

header["NachrichtenTyp"] = "2001"

switch header["NachrichtenTyp"] {
case "2001":
    println("2001 Import new file")

default:
    break 
}

I found this thread: Strings in Switch Statements: 'String' does not conform to protocol 'IntervalType'

But this example also don't work on my playground (Xcode 6.2)

Upvotes: 0

Views: 106

Answers (4)

zrzka
zrzka

Reputation: 21259

If you don't want to type as String many times in case of lot of different values, you can nest two switch statements like this:

var header: [String:AnyObject] = [:]

header["NachrichtenTyp"] = "2001"

for key in header.keys {

  switch header[key] {

    case let str as String:
      switch str {
        case "2001":
          println("2001 Import new file")
        default:
          break
      }

    case let num as Int:
      // ...
      break

    default:
      break
  }

}

Upvotes: 2

itinance
itinance

Reputation: 12418

Also you can cast your "NachrichtenTyp". So you dont need to "as string" each case-statement:

    var header: [String:String] = [:]

    header["NachrichtenTyp"] = "2001"

    let ntype:String = header["NachrichtenTyp"]! as String

    switch ntype {
    case "2001":
        println("2001 Import new file")

    default:
        break 
    }

Upvotes: 1

itsji10dra
itsji10dra

Reputation: 4677

Modify like this :

var header: [String:AnyObject] = [:]

header["NachrichtenTyp"] = "2001" ;

if let header = header["NachrichtenTyp"] as? NSString
{
    switch header
    {
        case "2001":
        println("2001 Import new file")
        break

    default:
        break
    }
}

Upvotes: 1

Kirsteins
Kirsteins

Reputation: 27345

You can solve this by:

case "2001" as String:
    println("2001 Import new file")

Upvotes: 2

Related Questions