How to get the first element from an array in Swift

2 Answers

0 votes
var arr = [5, 8, 0, 1, 4, 9, 5, 3]
  
let firstElement = arr[0]

print(firstElement) 
  
  
  
  
  
/*
run:
  
5
  
*/

 



answered Feb 18, 2021 by avibootz
0 votes
var arr = [5, 8, 0, 1, 4, 9, 5, 3]
  
let firstElement = arr.first ?? 0

print(firstElement) 
  
  
  
  
  
/*
run:
  
5
  
*/

 



answered Feb 18, 2021 by avibootz

Related questions

...