I'm using Playground in Xcode, and my objects aren't being initialized with their names. I feel like it's because I'm using the convenience init incorrectly in my sublcasses, and I was wondering what is the proper way to use them in subclasses. I've read the other similar questions, but I think my question is different in the way that it has overriding inits and convenience inits.
class Animal
{
var name:String
init(name:String)
{
self.name = name
}
convenience init() { self.init(name: "") }
func speak() { }
}
class Fox: Animal
{
override init(name: String)
{
super.init(name: name)
}
convenience init() { self.init(name: "Fox") }
override func speak()
{
println("Ring")
}
}
class Cat: Animal
{
override init(name: String)
{
super.init(name: name)
}
convenience init() { self.init(name:"Cat") }
override func speak() {
println("Meow")
}
}
class Dog: Animal {
override init(name: String) {
super.init(name: name)
}
convenience init()
{
self.init(name:"Dog")
}
override func speak() {
println("Woof")
}
}
let animals = [ Dog(), Cat(), Fox()]
for animal in animals
{
animal.speak()
}
There are no errors in your code. I think the problems is to understand how the Xcode's playground works. probably you had pressed "show results" icon and you're watching at something like this:
but this is telling us that the "Module name" dot "Class name" of the three animals; the prefix __lldb_expr_25 ( in my picture ) is not an error but a dynamic module name that is ok in playground.
Indeed you should look at the "assistant editor":
to see the output of speck() method:
This can be even more pronounced with a slight modification to the code:
so the output is: