Contents

take()

Takes the wrapped value being stored in this instance and returns it while also setting the instance to nil. If there is no value being stored in this instance, this returns nil instead.

Declaration

mutating func take() -> Optional<Wrapped>

Return Value

The wrapped value being stored in this instance. If this instance is nil, returns nil.

Discussion

var numberOfShoes: Int? = 34

if let numberOfShoes = numberOfShoes.take() {
  print(numberOfShoes)
  // Prints "34"
}

print(numberOfShoes)
// Prints "nil"