我有3个动物阵列:
let mammals = ["Dog", "Cat", "Bear"]
let fish = ["Clownfish", "Seahorse", "Scorpion Fish"]
let reptiles = ["Chameleon", "Snake", "Lizard"]
按钮随机生成动物,然后将其附加到allAnimals数组。我希望能够确定字符串(动物)来自哪个数组。
我尝试过使用
allAnimals.contains()
然而,显式地将var作为参数。我可以做allAnimals.contains(“海马”)就好了。但我需要能够检查所有阵列。
我也试过迭代数组。
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'
如何确定随机字符串来自哪个数组?
答案 0 :(得分:3)
您可以使用不同的动物类型定义枚举,并构建该枚举动物类型的数组,其中枚举(String
)中每个案例的相关值提供动物的详细信息。
E.g。
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 */
答案 1 :(得分:1)
反过来
UPDATE theTable t
CROSS join ( SELECT @tmp_slot1:=0, @tmp_slot2:=0, @tmp_slot3:=0) AS init
set
t.slot1 = @tmp_slot1 := t.slot1, -- save the val from slot1 into @tmp_slot1
t.slot2 = @tmp_slot2 := t.slot2, -- save the val from slot2 into @tmp_slot2
t.slot3 = @tmp_slot2 := t.slot3, -- save the val from slot3 into @tmp_slot3
t.slot1= CASE WHEN @tmp_slot1= '".$value."' THEN @tmp_slot1
WHEN @tmp_slot2= '".$value."' THEN @tmp_slot2
WHEN @tmp_slot3= '".$value."' THEN @tmp_slot3 END,
t.slot2= CASE WHEN @tmp_slot1= '".$value."' THEN @tmp_slot2
WHEN @tmp_slot2= '".$value."' THEN @tmp_slot1
WHEN @tmp_slot3 = '".$value."' THEN @tmp_slot2 END,
t.pokemon3 = CASE WHEN @tmp_slot1= '".$value."' THEN @tmp_slot3
WHEN @tmp_slot2= '".$value."' THEN @tmp_slot3
WHEN @tmp_slot3= '".$value."' THEN @tmp_slot1 END
WHERE id = '".$id."'";
答案 2 :(得分:0)
您需要检查每个数组是否包含随机选择的动物。
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")
}
答案 3 :(得分:0)
清洁协议方法(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