title: Define a read-only property in Swift
body: <p>How do you define a read-only property in Swift? I have one parent class which needs to define a public property eg. <code>itemCount</code>. Here's my code:</p>
  
  <pre><code>Class Parent: UIView {
    private(set) var itemCount: Int = 0
  }
  
  class Child {
    private(set) override var itemCount {
      get {
        return items.count
      }
    }
  }
  </code></pre>
  
  <p>I get the error: <code>Cannot override mutable property with read-only property</code></p>
  
  <hr>
  
  <h3>Option 1 - Protocols:</h3>
  
  <p>Well I can't use a protocol because they can't inherit from classes (<code>UIView</code>)</p>
  
  <h3>Option 2 - Composition:</h3>
  
  <p>I add a <code>var view = UIView</code> to my Child class and drop the <code>UIView</code> inheritance from my <code>Parent</code> class. This seems to be the only possible way, but in my actual project it seems like the wrong thing to do, eg. <code>addSubview(myCustomView.view)</code></p>
  
  <h3>Option 3 - Subclass <code>UIView</code> on the <code>Child</code> class</h3>
  
  <p>I can't do this either because I intend to have multiple related <code>Child</code> classes with different properties and behaviour, and I need to be able to declare instances of my <code>Child</code> classes as the <code>Parent</code> class to take advantage of <code>UIView</code>'s properties and <code>Parent</code>'s public properties.</p>