YouTip LogoYouTip

Swift Arrays

Swift arrays use an ordered list to store multiple values of the same type. The same value can appear multiple times at different positions in an array. Swift arrays enforce type checking of elements, and will report an error if the types are different. Swift arrays should follow the form like Array<Element>, where Element is the only data type allowed to exist in this array. If you create an array and assign it to a variable, the collection created is mutable. This means that after creating the array, you can change the items in the array by adding, removing, or modifying them. If you assign an array to a constant, the array is immutable, and its size and content cannot be modified. * * * ## Creating Arrays We can use the initializer syntax to create an empty array of a specific data type: var someArray = () The following is the syntax for creating an array with an initial size: var someArray = (repeating: InitialValue, count: NumbeOfElements) The following example creates an empty array of type Int, with a count of 3 and an initial value of 0: var someInts = (repeating: 0, count: 3) The following example creates an array containing three elements: var someInts: = [10, 20, 30] * * * ## Accessing Arrays We can access the elements of an array based on its index, the syntax is as follows: var someVar = someArray The index starts at 0, meaning index 0 corresponds to the first element, index 1 corresponds to the second element, and so on. We can learn how to create, initialize, and access arrays through the following example: import Cocoavar someInts = (repeating: 10, count: 3)var someVar = someIntsprint( "Value of the first element \(someVar!)" )print( "Value of the second element \(someInts!)" )print( "Value of the third element \(someInts!)" ) The output of the above program execution is: Value of the first element 10Value of the second element 10Value of the third element 10 * * * ## Modifying Arrays You can use the append() method or the addition assignment operator += to add elements to the end of an array. As shown below, we initialize an array and add elements to it: import Cocoavar someInts = () someInts.append(20) someInts.append(30) someInts += var someVar = someIntsprint( "Value of the first element \(someVar)" )print( "Value of the second element \(someInts)" )print( "Value of the third element \(someInts)" ) The output of the above program execution is: Value of the first element 20Value of the second element 30Value of the third element 40 We can also modify the value of an array element via its index: import Cocoavar someInts = () someInts.append(20) someInts.append(30) someInts += // Modify the last element someInts = 50var someVar = someIntsprint( "Value of the first element \(someVar)" )print( "Value of the second element \(someInts)" )print( "Value of the third element \(someInts)" ) The output of the above program execution is: Value of the first element 20Value of the second element 30Value of the third element 50 * * * ## Iterating Over Arrays We can use a for-in loop to iterate over all data items in an array: import Cocoavar someStrs = () someStrs.append("Apple") someStrs.append("Amazon") someStrs.append("Tutorial") someStrs += for item in someStrs { print(item)} The output of the above program execution is: AppleAmazonTutorialGoogle If we simultaneously need the value and index of each data item, we can use the enumerated() method of String to iterate over the array. The example is as follows: import Cocoavar someStrs = () someStrs.append("Apple") someStrs.append("Amazon") someStrs.append("Tutorial") someStrs += for (index, item) in someStrs.enumerated() { print("Value at index = \(index) is \(item)")} The output of the above program execution is: Value at index = 0 is AppleValue at index = 1 is AmazonValue at index = 2 is TutorialValue at index = 3 is Google * * * ## Merging Arrays We can use the addition operator (+) to merge two existing arrays of the same type. The data type of the new array will be inferred from the data types of the two arrays: import Cocoavar intsA = (repeating: 2, count:2)var intsB = (repeating: 1, count:3)var intsC = intsA + intsB for item in intsC { print(item)} The output of the above program execution is: 22111 * * * ## count Property We can use the count property to calculate the number of elements in an array: import Cocoavar intsA = (count:2, repeatedValue: 2)var intsB = (count:3, repeatedValue: 1)var intsC = intsA + intsB print("Number of elements in intsA is \(intsA.count)")print("Number of elements in intsB is \(intsB.count)")print("Number of elements in intsC is \(intsC.count)") The output of the above program execution is: Number of elements in intsA is 2 Number of elements in intsB is 3 Number of elements in intsC is 5 * * * ## isEmpty Property We can use the read-only property isEmpty to determine whether an array is empty, which returns a boolean value: import Cocoavar intsA = (count:2, repeatedValue: 2)var intsB = (count:3, repeatedValue: 1)var intsC = ()print("intsA.isEmpty = \(intsA.isEmpty)")print("intsB.isEmpty = \(intsB.isEmpty)")print("intsC.isEmpty = \(intsC.isEmpty)") The output of the above program execution is: intsA.isEmpty = false intsB.isEmpty = false intsC.isEmpty = true
← Swift DictionariesSwift Characters β†’