如何使用带枚举的switch作为字符串

时间:2018-03-18 06:24:59

标签: swift

很抱歉这个非常基本的问题,但我想弄清楚如何使用switch语句来检查它是否是某个字符串。

例如,如果我有一个AnimalType枚举,然后我有一个动物结构:

enum AnimalType: String {
   case Mammal = "Mammal"
   case Reptile = "Reptile"
   case Fish = "Fish"
}

struct Animal {
   let name: String
   let type: String
}

如果我想查看动物列表然后有一个switch语句,我如何将Animal.Type字符串与枚举匹配?我不想更改Animal结构以允许输入:AnimalType。

switch Animal.type {
case :
...// how do I match the string to the enum?

5 个答案:

答案 0 :(得分:2)

您可以从字符串rawValue创建一个动物类型并打开它: 但首先我将案例更改为小写,这是Swift中的首选样式。

func checkType(of animal: Animal) {
    guard let animalType = AnimalType(rawValue: animal.type) else {
        print("Not an animal type")
        return
    }
    switch animalType {
    case .mammal: break
    case .reptile: break
    case .fish: break
    }
}

或者,您也可以打开字符串并比较它是否与您的任何AnimalType rawValues匹配:

func checkType(of animal: Animal) {
    switch animal.type {
    case AnimalType.mammal.rawValue: break
    case AnimalType.reptile.rawValue: break
    case AnimalType.fish.rawValue: break
    default:
        print("Not an animal type")
        break
    }
}

答案 1 :(得分:1)

我建议您使用enum进行比较,并将其作为type的可选项。您还可以使用枚举的rawValue来比较它们。

enum AnimalType: String { 
    case Mammal //No need of case Mammal = "Mammal"
    case Reptile
    case Fish
}

struct Animal {
    let name: String
    let type: AnimalType?
}


let lion = Animal(name: "Lion", type: .Mammal)

switch lion.type {

case .Mammal?:
    break
case .Reptile?:
    break
case .Fish?:
    break
case nil:
    break

}


修改
正如Matthew在评论中所说,如果您从服务器获取对象,则需要使用自定义解码过程将字符串响应转换为相应的AnimalType枚举进行比较。否则你只使用枚举就好了。

答案 2 :(得分:0)

enum Command: String {
   case Mammal = "Mammal"
   case Reptile = "Reptile"
   case Fish = "Fish"
}

let command = Command(rawValue: "d")

switch command {
case .Mammal?:
    print("Mammal")
case .Reptile?:
    print("second")
case .Fish?:
    print("third")
case nil:
    print("not found")
}
// prints "not found"

答案 3 :(得分:0)

如果字符串与枚举完全相同,则不必定义它。以下是我的工作解决方案。

enum SegueIdentifier: String {
    case pushViewController1
    case pushViewController2
    case pushViewController3
}

switch SegueIdentifier(rawValue: segue.identifier ?? "") {
case .pushViewController1:
     if let viewController1 = segue.destination as? ViewController1 {
         viewController1.customProperty = customPropertyValue
     }
case .pushViewController2:
     if let viewController2 = segue.destination as? ViewController2 {
         viewController2.customProperty = customPropertyValue
     }
case .pushViewController3:
     if let viewController3 = segue.destination as? ViewController3 {
         viewController3.customProperty = customPropertyValue
     }
default:
    let message = "This case has not been handled."
    let alertController = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
    let okayAction = UIAlertAction(title: "Okay", style: .default) { (_:UIAlertAction) in }

    alertController.addAction(okayAction)

    present(alertController, animated: true, completion: nil)
}

答案 4 :(得分:0)

将以下代码粘贴到您的Playground中,以查看其工作原理。

您基本上可以创建一个包含动物的列表,并达到AnimalType的rawValue来检查列表中每只动物的开关何时匹配,如下所示:

import UIKit

enum AnimalType: String {
    case mammal = "Mammal"
    case reptile = "Reptile"
    case fish = "Fish"
    case cat = "Cat"
    case bug = "Bug"
}

struct Animal {
    let name: String
    let type: String
}

// Create some animals list
let animals: [Animal] = [Animal(name: "Kiwi", type: "Cat"),
                         Animal(name: "Copy", type: "Cat"),
                         Animal(name: "Oebi", type: "Cat"),
                         Animal(name: "Zaza", type: "Bug")]
// For each animal in the list
for animal in animals {
    // Check if its type actually matches a type from your enum
    switch animal.type {
    case AnimalType.cat.rawValue:
        print("\(animal.name) is a \(AnimalType.cat.rawValue)")
    case AnimalType.bug.rawValue:
        print("\(animal.name) is a \(AnimalType.bug.rawValue)")
    default:
        "This is not a cat nor a bug"
    }
    
}

除非您希望Animal结构的类型为AnimalType。如果是这样,您可以这样做:

import UIKit

enum AnimalType: String {
    case mammal = "Mammal"
    case reptile = "Reptile"
    case fish = "Fish"
    case cat = "Cat"
    case bug = "Bug"
}

struct Animal {
    let name: String
    let type: AnimalType
}

// Create some animals list
let animals: [Animal] = [Animal(name: "Kiwi", type: .cat),
                         Animal(name: "Copy", type: .cat),
                         Animal(name: "Oebi", type: .cat),
                         Animal(name: "Zaza", type: .bug)]
// For each animal in the list
for animal in animals {
    // Check if its type actually matches a type from your enum
    switch animal.type {
    case .cat:
        print("\(animal.name) is a \(AnimalType.cat.rawValue)")
    case .bug:
        print("\(animal.name) is a \(AnimalType.bug.rawValue)")
    default:
        "This is not a cat nor a bug"
    }

}