title: Swift 4: Best way to change the length of an array?
body: <pre><code>import Foundation
  
  public
  class Example001 {
      public
      func changeLength(of arrayOfInt: [Int], to newLength: Int) -&gt; [Int] {
          let oldLength = arrayOfInt.count
          if oldLength == newLength {
              return arrayOfInt
          }
  
          if oldLength &lt; newLength {
              var newArrayOfInt = [Int]()
              newArrayOfInt.append(contentsOf: arrayOfInt)
              for _ in oldLength ..&lt; newLength {
                  newArrayOfInt.append(0)
              }
              return newArrayOfInt
          }
  
          return Array(arrayOfInt.prefix(newLength))
      }
  }
  </code></pre>
  
  <p>This method changeLength() returns an array based on an existing array and a new length.</p>
  
  <p>The code works fine, whether the new array is shorter than, longer than, or the same length as the original array.</p>
  
  <p>But I am wondering if there is some better way of doing this in Swift 4 in a more compact way.</p>