title: Cannot assign to property: 'self' is immutable swift error
body: <p>Inside my app I created a Protocol called "Positions" and extended the UIView class to conform to this protocol in order to add some properties to the UIView class. I used the code below.</p>
  
  <pre><code>import Foundation
  import UIKit
  
  protocol Positions {
      var initialPositions: [CGRect] {get set}
      var finalPositions: [CGRect] {get set}
      var positionsAreDefined: Bool {get}
  }
  
  public enum PositionsType {
      case initial
  }
  
  private var initialPositionKey: UInt = 0
  private var finalPositionKey: UInt = 0
  
  extension Positions {
      var initialPositions: [CGRect] {
          get {
              if objc_getAssociatedObject(self, &amp;initialPositionKey) != nil {
                  return objc_getAssociatedObject(self, &amp;initialPositionKey) as! [CGRect]
              } else {
                  return [] as! [CGRect]
              }
  
          }
          set {
              objc_setAssociatedObject(self, &amp;initialPositionKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
          }
      }
  
      var finalPositions: [CGRect] {
          get {
              if objc_getAssociatedObject(self, &amp;finalPositionKey) != nil {
                  return objc_getAssociatedObject(self, &amp;finalPositionKey) as! [CGRect]
              } else {
                  return [] as! [CGRect]
              }
          }
  
          set {
              objc_setAssociatedObject(self, &amp;finalPositionKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
          }
      }
  
      var positionsAreDefined: Bool {
          get {return initialPositions.count != 0 &amp;&amp; finalPositions.count != 0 &amp;&amp; initialPositions.count == finalPositions.count}
      }
  
      var positionsCount: Int {
          get {return initialPositions.count}
      }
  }
  
  extension UIView: Positions {}
  </code></pre>
  
  <p>Well, today I tried to extend the UIView class with a new method that changes the added-by-me properties but the compiler gave me this error on the line where I tried to modify the properties value:</p>
  
  <blockquote>
    <p>Cannot assign to property: 'self' is immutable</p>
  </blockquote>
  
  <p>Here's the method:</p>
  
  <pre><code>public func horizontallyInContainer(withShift shift: CGFloat, forExpansionNumber index: Int, forPositionType type: PositionsType) {
      if self.positionsAreDefined &amp;&amp; self.superview != nil {
          if type == .initial {
  
              var newPositions = self.initialPositions
              newPositions[index].origin.x = self.superview!.bounds.width * 0.5 - self.bounds.width + shift
              self.initialPositions = newPositions //Here happens the error
  
          } else {
  
              var newPositions = self.finalPositions
              newPositions[index].origin.x = self.superview!.bounds.width * 0.5 - self.bounds.width + shift
              self.finalPositions = newPositions //Also here happens
  
          }
      }
  }
  </code></pre>
  
  <p>Can anyone explain me the nature of that error?</p>