Google News
logo
Swift - Interview Questions
List down three ways in which we can append two arrays in ios Swift.
Let us consider that the two arrays are declared as follows:
var firstArray = ["Ramana", "Raani"]
let secondArray = ["Srikanth", "Anusha"]
A thing to be noted is that the first array has been kept mutable so we can append the second array to it. The three ways in which we can append the second array to the first one are as follows:
 
* Using the method "append(contentsOf: )" - In this method, the contents of the second array are copied into the first array.
firstArray.append(contentsOf: secondArray)
* Using the "+=" operator - In this method also, the contents of the second array are copied into the first array.
firstArray += secondArray
* Appending two arrays by using the "+" operator and adding the result to a new array -
let thirdArray = firstArray + secondArray
Advertisement