确定字符串来自哪个数组
Determining which array a string came from
我有 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'
如何确定随机字符串来自哪个数组?
反过来
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") }
}
您需要检查每个数组是否包含随机选择的动物。
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")
}
您可以用不同的动物类型定义一个枚举,并构造该枚举动物类型的数组,其中枚举中每个案例的关联值 (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 */
更简洁的协议方法(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({[=10=] is Fish})
print(fish)
// Clownfish, Seahorse, Scorpion Fish
let mammals = everyAnimal.filter({[=10=] is Mammal})
print(mammals)
// Dog, Cat, Bear
let reptiles = everyAnimal.filter({[=10=] is Reptile})
print(reptiles)
// Chameleon, Snake, Lizard
我有 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'
如何确定随机字符串来自哪个数组?
反过来
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") }
}
您需要检查每个数组是否包含随机选择的动物。
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")
}
您可以用不同的动物类型定义一个枚举,并构造该枚举动物类型的数组,其中枚举中每个案例的关联值 (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 */
更简洁的协议方法(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({[=10=] is Fish})
print(fish)
// Clownfish, Seahorse, Scorpion Fish
let mammals = everyAnimal.filter({[=10=] is Mammal})
print(mammals)
// Dog, Cat, Bear
let reptiles = everyAnimal.filter({[=10=] is Reptile})
print(reptiles)
// Chameleon, Snake, Lizard