【问题标题】:Determining which array a string came from确定字符串来自哪个数组
【发布时间】:2017-05-06 00:33:19
【问题描述】:

我有 3 个动物阵列:

let mammals = ["Dog", "Cat", "Bear"]
let fish = ["Clownfish", "Seahorse", "Scorpion Fish"]
let reptiles = ["Chameleon", "Snake", "Lizard"]

一个按钮随机生成一个动物,然后将它附加到一个 allAnimals 数组中。我希望能够确定字符串(动物)来自哪个数组。

我尝试过使用

allAnimals.contains()

但是,它明确地将 var 作为参数。我可以做 allAnimals.contains("Seahorse") 就好了。但我需要能够检查所有数组。

我也尝试过遍历数组。

for i in allAnimals {
  if i.contains(mammals){ print("Came from the Mammal array") }
  else if i.contains(fish){ print("Came from the Fish array") }
  else if i.contains(reptiles){ print("Came from the Reptiles array") }
}

这引发了错误:

Cannot convert value of type '[String]' to expected argument type 'String'

如何确定随机字符串来自哪个数组?

【问题讨论】:

  • 您可以为每种类型创建一个结构,然后检查它是哪种类型。您还可以为每种类型创建一个带有关联值的枚举。
  • 非常感谢这里的所有建议,伙计们。 @vadian 的解决方案对我来说效果很好,但看到其他可以完成的方法对我很有帮助。

标签: arrays swift loops contains


【解决方案1】:

您可以使用不同的动物类型定义一个枚举,并构造该枚举动物类型的数组,其中枚举中每个案例的关联值 (String) 给出了动物的详细信息。

例如

enum Animal: CustomStringConvertible {
    case mammal(String)
    case fish(String)
    case reptile(String)

    var description: String {
        switch self {
        case .mammal: return "Mammal"
        case .fish: return "Fish"
        case .reptile: return "Reptile"
        }
    }

    var species: String {
        switch self {
        case .mammal(let s), .fish(let s), .reptile(let s): return s
        }
    }
}

示例用法:

let mammals = ["Dog", "Cat", "Bear"].map(Animal.mammal)
let fish = ["Clownfish", "Seahorse", "Scorpion Fish"].map(Animal.fish)
let reptiles = ["Chameleon", "Snake", "Lizard"].map(Animal.reptile)

let allAnimals = [reptiles[2], mammals[1]]

for animal in allAnimals {
    print("\(animal.species) is a \(animal)")
} /* Lizard is a Reptile
     Cat is a Mammal     */

【讨论】:

    【解决方案2】:

    反过来

    for anAnimal in allAnimals { // with a bit more descriptive index variable
      if mammals.contains(anAnimal){ print("Came from the Mammal array") }
      else if fish.contains(anAnimal){ print("Came from the Fish array") }
      else if reptiles.contains(anAnimal){ print("Came from the Reptiles array") }
    }
    

    【讨论】:

    • 这成功了!!谢谢!我会在 5 分钟内接受这个作为答案。
    【解决方案3】:

    您需要检查每个数组是否包含随机选择的动物。

    let mammals = ["Dog", "Cat", "Bear"]
    let fish = ["Clownfish", "Seahorse", "Scorpion Fish"]
    let reptiles = ["Chameleon", "Snake", "Lizard"]
    
    let chosenAnimal = "Cat"
    
    if mammals.contains(chosenAnimal) {
        print("mammal")
    } else if fish.contains(chosenAnimal) {
        print("fish")
    } else if reptiles.contains(chosenAnimal) {
        print("reptile")
    } else {
        print("animal not in any arrays")
    }
    

    【讨论】:

      【解决方案4】:

      更简洁的协议方法(Swift 3)

      所以基本上正如其他人所建议的那样......使用数组来确定它属于哪个组并不是真正合适的设计。但是,如果它非常简单并且您确定它不需要扩展,那么您的方法实际上可能就可以了。

      protocol Animal: CustomStringConvertible {
          var description: String { get }
      }
      protocol Mammal: Animal {}
      protocol Fish: Animal {}
      protocol Reptile: Animal {}
      
      class Dog: Mammal { var description: String = "Dog" }
      class Cat: Mammal { var description: String = "Cat" }
      class Bear: Mammal { var description: String = "Bear" }
      
      class Clownfish: Fish { var description: String = "Clownfish" }
      class Seahorse: Fish { var description: String = "Seahorse" }
      class ScorpionFish: Fish { var description: String = "Scorpion Fish" }
      
      class Chameleon: Reptile { var description: String = "Chameleon" }
      class Snake: Reptile { var description: String = "Snake" }
      class Lizard: Reptile { var description: String = "Lizard" }
      
      let everyAnimal:[Animal] = [Dog(), Cat(), Bear(), Clownfish(), Seahorse(), ScorpionFish(), Chameleon(), Snake(), Lizard()]
      let fish = everyAnimal.filter({$0 is Fish})
      print(fish)
      // Clownfish, Seahorse, Scorpion Fish
      let mammals = everyAnimal.filter({$0 is Mammal})
      print(mammals)
      // Dog, Cat, Bear
      let reptiles = everyAnimal.filter({$0 is Reptile})
      print(reptiles)
      // Chameleon, Snake, Lizard
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-19
        • 2012-07-20
        • 1970-01-01
        • 1970-01-01
        • 2016-08-14
        • 2020-03-06
        • 2018-02-13
        • 1970-01-01
        相关资源
        最近更新 更多