我想创建一个泛型函数,它将泛型数组作为参数。我有两个类Animal和Bird以及两个协议Animal& Birds和我的方法参数符合这两个协议,但我无法添加到数组中。
protocol Birds {
var canFly: Bool {get set}
}
protocol Animals {
var name: String {get set}
var legs: Int {get set}
}
class Animal: Animals {
var name: String
var legs: Int
init(name: String, legs: Int) {
self.name = name
self.legs = legs
}
}
class Bird: Birds {
var canFly: Bool
init(canFly: Bool) {
self.canFly = canFly
}
}
func myTestGenericMethod<T>(array: [T]) where T: Animals & Birds {
for (index, _) in array.enumerated() {
print("At last i am able to get both Animal and Bird")
}
}
let cat = Animal(name: "cat", legs: 4)
let dog = Animal(name: "dog", legs: 4)
let crow = Bird(canFly: true)
myTestGenericMethod(array: [dog])
myTestGenericMethod(array: [cat, dog]) // Not Able to add this to array
答案 0 :(得分:2)
当您撰写where T: Animals & Birds
时,T
必须从Animals
和 Birds
但cat
和dog
并未从Animals
AND Birds
扩展。所以这就是问题。
据我了解,您希望T
必须从Animals
或 Birds
进行扩展。要做到这一点,我们必须有一个Animals
和Birds
扩展的基本协议。更改一些代码并修复它。
@objc protocol Base {
}
protocol Birds : Base {
var canFly: Bool {get set}
}
protocol Animals : Base {
var name: String {get set}
var legs: Int {get set}
}
class Animal: Animals {
var name: String
var legs: Int
init(name: String, legs: Int) {
self.name = name
self.legs = legs
}
}
class Bird: Birds {
var canFly: Bool
init(canFly: Bool) {
self.canFly = canFly
}
}
func myTestGenericMethod<T: Base>(array: [T]) {
for object in array {
if object is Bird {
let bird = object as! Bird
print(bird.canFly)
} else if object is Animal {
let animal = object as! Animal
print(animal.name)
}
}
}
let cat = Animal(name: "cat", legs: 4)
let dog = Animal(name: "dog", legs: 4)
let crow = Bird(canFly: true)
myTestGenericMethod(array: [crow, cat, dog] as! [Base])
myTestGenericMethod(array: [cat, dog])
答案 1 :(得分:1)
在您的代码中where T: Animals & Birds
表示您需要T
同时成为符合这两种协议的实例。但是你没有符合这两种协议的类。如果你创建一个,那么你将能够在你的泛型方法中使用它的实例。