Swift Collection Type Array
- Collection Types in Swift
- Swift has 3 primary collection types , known as arrays, sets and dictionaries
- arrays – ordered collections of values
- sets – unordered collection of values
- dictionaries – unordered collections of key-value pairs
In this post we will see how Swift Array works
Swift Array
func arrayTest1(){ var intArray = [Int]() //This line creates an empty array print(intArray) //[] intArray.append(10) print(intArray) //[10] intArray.append(20) print(intArray) //[10, 20] intArray = [] print(intArray) //[] - At this moment the intArray is again empty }
Swift – Creating a float array with the same repeated value
func arrayTest2(){
let floatArray = [Float] (count: 3, repeatedValue: 12.32)
print(floatArray) //[12.32, 12.32, 12.32]
}
Swift Adding Two Arrays to create a third array
func arrayTest3(){ let array1 = [10,20] let array2 = [30,40] //Adding two arrays to create a new array let array3 = array1 + array2 print(array3) //[10, 20, 30, 40] }
Creating an array using an Array literal
func arrayTest4(){ let nameArray:[String] = ["John Doe","Jane Doe","Mary Jane"] let ageArray:[Int] = [22,23,21] print(nameArray) //[John Doe, Jane Doe, Mary Jane] print(ageArray) //[22, 23, 21] //If we are initializing an array with the array literal then we do not have to write the type so for the above example can be re-written as let nameArray1 = ["John Doe","Jane Doe","Mary Jane"] //Note we have not used the array type print(nameArray1) //[John Doe, Jane Doe, Mary Jane] //In the above since all the objects are of the same type, Swift can infer that the type of the array is [String] }
Accessing and Modifying an Array
func arrayTest5(){ var monthArray = ["Jan","Feb"] print("monthArray Count is \(monthArray.count)") //monthArray Count is 2 if (monthArray.isEmpty){ print("Month Array is empty") } else{ print("Month Array is not empty") } //Month Array is not empty monthArray.append("Mar") print(monthArray) //[Jan, Feb, Mar] monthArray += ["Apr","May"] print(monthArray) //[Jan, Feb, Mar, Apr, May] let firstMonth = monthArray[0] //First item of an array is present at index as Swift Arrays are zero indexed print("first month is \(firstMonth)") //first month is Jan //Change the value of the first object of the array monthArray[0] = "January" print(monthArray) //[January, Feb, Mar, Apr, May] print("first month is \(monthArray[0])") //first month is January //Please note in the example given below the replacement set has a different length than the range we are trying to replace monthArray[1...4] = ["February","March","April","May","June","July"] print(monthArray) //[January, February, March, April, May, June, July] //Removing an item at an index monthArray.removeAtIndex(0) print(monthArray) //[February, March, April, May, June, July] //Inserting an item at an index monthArray.insert("January", atIndex: 0) print(monthArray) //[January, February, March, April, May, June, July] //Removing the last object of an array monthArray.removeLast() print(monthArray) //[January, February, March, April, May, June] //Note July got removed //Iterating over an array for month in monthArray{ print(month) } /* January February March April May June */ //Getting the index of each element of an array while iterating over it for (index, value) in monthArray.enumerate(){ print("index is \(index) and month is \(value)") } /* index is 0 and month is January index is 1 and month is February index is 2 and month is March index is 3 and month is April index is 4 and month is May index is 5 and month is June */ }
Leave a Reply