title: Swift: Creating an Array with a Default Value of distinct object instances
body: <p>I noticed a bit weird (<em>and dangerous  IMHO</em>) behavoir in <strong>Creating an Array with a Default Value</strong>. As stated  in <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html">Swift 2.1: Collection Types</a></p>
  
  <blockquote>
    <p>Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue):</p>
  </blockquote>
  
  <p>The point is: <strong>same default value</strong>; in order to understand how it work, I tried to create an array of elements of this example class</p>
  
  <pre><code>class User {
    private struct Shared {
      static var sequence: Int = 0
    }
  
    var id: Int
    var thinkTime: NSTimeInterval // typealias di Double
  
    init (thinkTime: NSTimeInterval) {
      User.Shared.sequence = User.Shared.sequence+1
      id = User.Shared.sequence
      self.thinkTime = thinkTime
    }
  }
  </code></pre>
  
  <p>and this testing code:</p>
  
  <pre><code>let  howManyUsers: Int = 3
  var users = [User](count: howManyUsers, repeatedValue:User(thinkTime: 10.0))
  let u2: User = User(thinkTime: 10)
  let u3: User = User(thinkTime: 10)
  users.append(u2)
  users.append(u3)
  users[1].thinkTime = 20
  users[3].thinkTime = 30
  
  for u in users {
    print("User id:\(u.id) thinktime:\(u.thinkTime)")
  }
  </code></pre>
  
  <p>gives:</p>
  
  <pre><code>User id:1 thinktime:20.0     
  User id:1 thinktime:20.0
  User id:1 thinktime:20.0
  User id:2 thinktime:30.0
  User id:3 thinktime:10.0
  </code></pre>
  
  <p>that definitively proof the initializer with the number of items to be added to the new array and a default value of the appropriate type are: <strong>the same object instance</strong></p>
  
  <p>Which is the way, as concise and smart as possible, to obtain a array of <strong>distinct object instances</strong> , instatiated with the same default value ( not the same instance but a number of instances initialized with the same default value ) ?</p>