title: Swift: Override object method
body: <p>I'm defining a class:</p>
  
  <pre><code>class Person {
      func speak(phrase: String) {
          NSLog("\(phrase)")
      }
  }
  </code></pre>
  
  <p>Suppose that I want to change the behavior of the <code>speak</code> method. In java I would override this method:</p>
  
  <pre><code>Person p = new Person() {
      @Override
      public void speak(String phrase) {
          // Do something different
      }
  };
  </code></pre>
  
  <p>How can I override the method in Swift? I tried the following to no avail:</p>
  
  <pre><code>let person = Person()
  person.speak = { (String) -&gt; () in
      // Do something different
  }
  </code></pre>
  
  <p>Error: <code>Cannot assign to 'speak' in 'person'</code></p>
  
  <p>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.</p>