How to remove the last element from an array and return the element in Swift

2 Answers

0 votes
var arr = ["swift", "c++", "php", "java"]
 
if let last = arr.popLast() {
    print(last)
    print(arr)
}

  
  
  
/*
run:

java
["swift", "c++", "php"]
 
*/

 



answered Sep 2, 2020 by avibootz
0 votes
var arr = [3, 6, 1, 8, 4]

let last = arr.removeLast() 

print(last)
print(arr)
 

  
  
/*
run:

4
[3, 6, 1, 8]
 
*/

 



answered Sep 2, 2020 by avibootz
...