Mastering macOS Programming
上QQ阅读APP看书,第一时间看更新

Methods that mutate and return a value

The following methods all return the elements that they remove from the array, as well as mutating the array:

let thirdElement = arr.remove(at: 2) 
let firstElement = arr.removeFirst()
let lastElement = arr.removeLast()

The methods above will cause a runtime error if called on an empty array. However, the following method returns an optional value, which will be nil if the array is empty:

let lastInt = arr.popLast() 

The lastInt value will need to be unwrapped before being used as an Int.