In SWIFT, An array is a collection of data of a similar type.

As a Swift developer, most of the time, you will interact with arrays, and hence it’s necessary to know the most used swift operations/methods.

Note: Like most of the other programming languages, the array index always starts with 0. Hence, the first element of an array is present at index 0, not 1.

Here are the few important array operations you should know,

Array.first – This returns an optional containing the first value in the array, or nil if the array is empty.

var stocks : [String] = ["Apple", "Google", "Facebook"]

if let firstStock = stocks.first {
    print (firstStock)
}

The above code print the value,

Apple

Array.last – This returns an optional containing the last value in the array, or nil if the array is empty.

var stocks : [String] = ["Apple", "Google", "Facebook"]

if let lastStock = stocks.last {
    print (lastStock)
}

The above code print the value,

Facebook

Array.count – This returns an Int of the length of the total number of the elements in the array.

var stocks : [String] = ["Apple", "Google", "Facebook"]

print(stocks.count)

The above code prints,

3

Array.isEmpty – This returns a Bool, whether the array is empty or not.

var stocks : [String] = ["Apple", "Google", "Facebook"]

if stocks.isEmpty {
    print("No stocks")
} else {
    print("\(stocks.count) stocks available")
}

The above code prints,

3 stocks available

Array. append – This appends a new element in the last+1 index.

var stocks : [String] = ["Apple", "Google", "Facebook"]

//Array.append(newElement: String>)
stocks.append("Amazon") // ["Apple", "Google", "Facebook", "Amazon"]
stocks.append("Twitter") // ["Apple", "Google", "Facebook", "Amazon", "Twitter"]
stocks.append("PayPal") // ["Apple", "Google", "Facebook", "Amazon", "Twitter", "PayPal"]
stocks.append("Tesla") // ["Apple", "Google", "Facebook", "Amazon", "Twitter", "PayPal", "Tesla"]

print(stocks)

The above code prints,

[“Apple”, “Google”, “Facebook”, “Amazon”, “Twitter”, “PayPal”, “Tesla”]

Array.insert – This will insert an element into the array at a specified index.

var stocks : [String] = ["Apple", "Google", "Facebook"]

stocks.insert("Amazon", at: 0) // ["Amazon", "Apple", "Google", "Facebook"]
stocks.insert("Twitter", at: 4) // ["Amazon", "Apple", "Google", "Facebook", "Twitter"]
stocks.insert("Tesla", at: 2) // ["Amazon", "Apple", "Tesla", "Google", "Facebook", "Twitter"]
stocks.insert("PayPal", at: 5) //["Amazon", "Apple", "Tesla", "Google", "Facebook", "PayPal", "Twitter"]

print(stocks)

The above code prints,

[“Amazon”, “Apple”, “Tesla”, “Google”, “Facebook”, “PayPal”, “Twitter”]

Array.removeLast – This will remove the last element from the array.

var stocks : [String] = ["Apple", "Google", "Facebook", "Amazon", "Twitter", "PayPal", "Tesla"]


print(stocks.removeLast())
print(stocks)

The above code prints,

Tesla // Returned the removed element from the array

[“Apple”, “Google”, “Facebook”, “Amazon”, “Twitter”, “PayPal”]

Array.removeAt – This will remove an element from the array at a specified index.  This will return the removed value.

var stocks : [String] = ["Apple", "Google", "Facebook", "Amazon", "Twitter", "PayPal", "Tesla"]


print(stocks.remove(at: 5)) 
print(stocks)

The above code prints,

PayPal // Returned the removed element at the index 5

[“Apple”, “Google”, “Facebook”, “Amazon”, “Twitter”, “Tesla”]

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *