Open In App

Swift – Arrays Properties

Improve
Improve
Like Article
Like
Save
Share
Report

An array is one of the most commonly used data types because of the benefits it provides in writing programs more efficiently. Just like other programming languages, in the Swift language, also array is a collection of similar data types. It stores data in an order list. When you assign an array to a variable, then your variable is mutable which means you can remove, add, or modify the elements present in the array. And when you assign an array to a constant, then your array will be immutable which means you cannot modify and change the size of the array. Before understanding the properties of the array first of all we understand how to create array:

var myarray = [TypeOfArray](count: NumbeOfItems, repeatedValue: Value)

or

var myarray:[Int] = [29, 17, 10]

or 

var myarray:Array<Int> = Array()  

Example:

var language = ["C", 'language","Java"] 
var num = [Int](count: 5, repeatedValue: 0)

Now some of the commonly used array properties are:

  • Array.count Property
  • Array.first Property
  • Array.last Property
  • Array.isEmpty Property

count Property

count property is used to count the total number of elements either be it numbers or strings present in the given array. 

Syntax:

Array.count

Example:

Swift




// Swift program to illustrate the use of count property
  
// Creating and initializing array variables
let myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]
let myArray2:[String] = ["C++", "Java", "Python", "Perl", "Go"]
let myArray3:[Int] = [3, 45, 3, 86, 50]
  
// Using count property to count the
// elements present in each array
let res1 = myArray1.count
let res2 = myArray2.count
let res3 = myArray3.count
  
// Displaying the total count
print("Total elements present in first array:\(res1)")
print("Total elements present in second array:\(res2)")
print("Total elements present in third array:\(res3)")


Output:

Total elements present in first array:7
Total elements present in second array:5
Total elements present in third array:5

first Property

first property is used to find the first element either be it numbers or strings present in the specified array.

Syntax:

Array.first

Example:

Swift




// Swift program to illustrate the use of first property
  
// Creating and initializing array variables
let myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]
let myArray2:[String] = ["C++", "Java", "Python", "Perl", "Go"]
let myArray3:[Int] = [3, 45, 3, 86, 50]
  
// Using first property to
// find the first element
// of the given array
let res1 = myArray1.first
let res2 = myArray2.first
let res3 = myArray3.first
  
// Displaying the first element
print("First element of the first array:\(res1)")
print("First element of the second array:\(res2)")
print("First element of the third array:\(res3)")


Output:

First element of the first array:Optional(23)
First element of the second array:Optional("C++")
First element of the third array:Optional(3)

last Property

last property is used to find the last element either be it numbers or strings present in the specified array.

Syntax:

Array.last

Example:

Swift




// Swift program to illustrate the use of last property
  
// Creating and initializing array variables
let myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]
let myArray2:[String] = ["C++", "Java", "Python", "Perl", "Go"]
let myArray3:[Int] = [3, 45, 3, 86, 50]
  
// Using last property to find the last element
// of the given array
let res1 = myArray1.last
let res2 = myArray2.last
let res3 = myArray3.last
  
// Displaying the last element
print("Last element of the first array:\(res1)")
print("Last element of the second array:\(res2)")
print("Last element of the third array:\(res3)")


 

Output:

Last element of the first array:Optional(34)
Last element of the second array:Optional("Go")
Last element of the third array:Optional(50)

isEmpty Property

isEmpty property is used to find whether the specified array is empty or not. This property will return true if the specified array is empty. Otherwise it will return false if the specified array is not empty.

Syntax:

Array.isEmpty

Example:

Swift




// Swift program to illustrate the use of isEmpty property
  
// Creating and initializing array variables
let myArray1:[Int] = [23, 45, 67, 78, 65, 32, 34]
let myArray2:[String] = ["C++", "Java", "Python", "Perl", "Go"]
let myArray3:[Int] = [Int]()
  
// Using isEmpty property to check
// whether the given array is empty or not
let res1 = myArray1.isEmpty
let res2 = myArray2.isEmpty
let res3 = myArray3.isEmpty
  
// Displaying the result
print("Is the first array is empty?:\(res1)")
print("Is the second array is empty?:\(res2)")
print("Is the third array is empty?:\(res3)")


 

Output:

Is the first array is empty?:false
Is the second array is empty?:false
Is the third array is empty?:true


Last Updated : 06 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads