title: Swift and mutating struct
body: <p>There is something that I don't entirely understand when it comes to mutating value types in Swift.</p>
  
  <p>As the "The Swift Programming Language" iBook states:  <strong><em>By default, the properties of a value type cannot be modified from within its instance methods.</em></strong></p>
  
  <p>And so to make this possible we can declare methods with the <code>mutating</code> keyword inside structs and enums.</p>
  
  <p>The thing that is not entirely clear to me is this:
  You can change a var from outside a struct, but you cannot change it from its own methods. This seems counter-intuitive to me, as in Object Oriented languages, you generally try to encapsulate variables so they can only be changed from within. With structs this appears to be the other way around. To elaborate, here's a code snippet:</p>
  
  <pre><code>struct Point {
      var x = 0, y = 0
      mutating func moveToX(x: Int, andY y:Int) { //Needs to be a mutating method in order to work
          self.x = x
          self.y = y
      }
  }
  
  var p = Point(x: 1, y: 2)
  p.x = 3 //Works from outside the struct!
  p.moveToX(5, andY: 5) 
  </code></pre>
  
  <p>Does anyone know the reason why structs cannot change their content from inside their own context, while the contents can easily be changed elsewhere?</p>