title: NSLayoutConstraint ivar init with self as an item in UIView subclass in Swift 1.2
body: <p>I have a subclass of UIView which uses some NSLayoutConstraint instances declared as ivars using Swift. The reason for them to be ivars is that I change the constant value. One of them is related to self: </p>
  
  <pre><code>NSLayoutConstraint(item: self.contentView, attribute: .Right, 
                     relatedBy: .Equal, toItem: self, attribute: .Right, 
                     multiplier: 1, constant: self.contentViewWidth)
  </code></pre>
  
  <p>As you know, all ivars should be initialized before calling to a superclass init, but you can't refer to self only after superclass init. Also, you can't change the toItem (second item) value after the NSLayoutConstraint instance has been initialized.</p>
  
  <p>Before Swift 1.2 marking an ivar with an exclamation mark (implicitly unwrapped value) told the compiler that you're responsible for this ivar to be non-nil when needed, so you can initialize the ivar wherever you want until you're sure it won't be used before that code has been called. That's what I used: I made this NSLayoutConstraint implicitly unwrapped and initialized just after the super.init call, so the self could be referenced:</p>
  
  <pre><code>class FilterView: UIView, UITableViewDelegate, UITableViewDataSource {
  
      private let contentRightConstraint : NSLayoutConstraint!
  
      init(frame: CGRect, customMode: Bool = false) {
          super.init(frame: CGRectZero)
          contentRightConstraint = NSLayoutConstraint(item: self.contentView, 
          attribute: .Right, relatedBy: .Equal, toItem: self, 
          attribute: .Right, multiplier: 1, constant: self.contentViewWidth)
      }
  }
  </code></pre>
  
  <p>Since Swift 1.2 the solution above doesn't work anymore. I found two workarounds for this:</p>
  
  <p>1) replace let with var and use a dummy value:</p>
  
  <pre><code>class FilterView: UIView, UITableViewDelegate, UITableViewDataSource {
  
      private var contentRightConstraint = NSLayoutConstraint()
  
      init(frame: CGRect, customMode: Bool = false) {
      super.init(frame: CGRectZero)
      contentRightConstraint = NSLayoutConstraint(item: self.contentView, 
          attribute: .Right, relatedBy: .Equal, toItem: self, 
          attribute: .Right, multiplier: 1, constant: self.contentViewWidth)
      }
  }
  </code></pre>
  
  <p>It's not good because it make the ivar changeable and creates an unnecessary instance.</p>
  
  <p>2) Use a lazy var:</p>
  
  <pre><code>class FilterView: UIView, UITableViewDelegate, UITableViewDataSource {
  
      private lazy var contentRightConstraint : NSLayoutConstraint = 
          NSLayoutConstraint(item: self.contentView, attribute: .Right, 
          relatedBy: .Equal, toItem: self, attribute: .Right, 
          multiplier: 1, constant: self.contentViewWidth)
  }
  </code></pre>
  
  <p>This is better, but still is changeable though should be <code>let</code>.</p>
  
  <p>Does anybody have other ideas?</p>