monekypox121 monekypox121 - 1 year ago 182
Swift Question

Extension: error use of undeclared type

I am new to swift, and I would like to create an extension of the Dog class:

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
}

public class Dog {
var name = "Timmy"
}
}

extension Dog {
func description() -> String {
return "A dog named \(self.name)"
}
}


I thought extensions go at the way bottom, can someone help me with this?

Answer Source

Your Dog class is "hidden" inside your ViewController class. Declare it at top level or refer to it as ViewController.Dog.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download