I'm defining a class:
class Person {
func speak(phrase: String) {
NSLog("\(phrase)")
}
}
Suppose that I want to change the behavior of the speak method. In java I would override this method:
Person p = new Person() {
@Override
public void speak(String phrase) {
// Do something different
}
};
How can I override the method in Swift? I tried the following to no avail:
let person = Person()
person.speak = { (String) -> () in
// Do something different
}
Error: Cannot assign to 'speak' in 'person'
I know I could set a variable to change the behavior or subclass Person, but I'm thinking of a different use case where something like this is needed. Using this example was easier to explain.