Reputation: 302
I have Animal class with "class func create()" :
class func create<T>() -> T {
let animal = Animal() as! T
return animal
}
and I have subclass Dog:
class Dog: Animal { }
I use create() for creating subclass objects:
let doggy: Dog = Dog.create()
But if I create doggy this way:
let doggy = Dog.create()
I have error "Generic parameter 'T' could not be inferred" How can I setup dynamic type inferred based on caller type?
Upvotes: 0
Views: 81
Reputation: 2727
this line:
let doggy = Dog.create()
declares a constant, without explicitly naming its type, yet, create function requires it. Try:
let doggy: Dog = Dog.create()
Upvotes: 0