Arrays in Swift with Examples

In this article, We are going to cover Arrays in Swift with Examples, creating arrays in swift, Accessing and Printing an Array in Swift, Concatenate two arrays in swift, Iterating two arrays in Swift, Enumerate arrays in swift.

Arrays in Swift

Introduction

Arrays are nothing but collection of similar types of elements stored at single place. There are various examples of arrays Number array, character array, array of vegetables. Content of arrays are called as array elements.

For example , Array of vegetable contains elements like potato, tomato, carrot, chili etc.

Creating arrays in swift:

To create an empty array:

var ARRAY NAME = [DATA TYPE] ()

In above syntax var is nothing but declaration of array. ARRAY NAME will be the name given by user to array .DATATYPE is depending on type of array we are going to create like for integer array Data type will be INT.

To Create an Array in Swift of Particular Size

var ARRYA NAME = [SomeType](count: NumbeOfElements, repeatedValue:         InitialValue)

In above syntax, Count represents total number of elements to be added in array means it is nothing but size of an array. repeatdValue represents starting value/ initial value

Example:

var ABC = [Int](count: 3, repeatedValue: 0)
       var someInts:[Int] = [10, 20, 30]

In above example first line represents declaration of array which contains array name as ABC with number of elements 3 and initial/starting address value 0 , Second line contains actual elements present in array.

Accessing and Printing an Array in Swift

If we want to search any element in array or print any element of an array. Following syntax is used. Here index is nothing both location of an element/ address of an element.

var ABC = ABC[index]

 Example:

var ABC = [Int](count: 3, repeatedValue: 10)
var ABCvar = someInts[0]
print( "Value of first element is \(ABC[0])”)

Output:

Value of first element is 10

In above example first line is for array declaration as discussed above. Second line represent element to be accessed. Element at 0 memory location need to access. Third line is syntax for printing that element which is located at first address location.

let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let array3 = array1 + array2
print(array3)

Above code represents adding of array elements. Here two arrays are taken of size 3, array1 and array2 respectively.

Third line in above code represents addition of elements in array1 and array2 . Whereas last line is used to print addition of two arrays.

Concatenate two arrays in swift

var array1:[Int] = [22, 54] 
 
var array2:[Int] = [35, 68] 
 
var new_array = array1 + array2
 
for str in new_array { 
    print( "\(str)" ) 
} 

Output:

22
54
35
68

Above code represents concatenation of two array elements. Here elements addition operation will not happen. But elements from two arrays get appended. For example, In above example Array1 and array2 are taken as an input. One new array is introduced that is new_array in which appended elements will be kept.

Iterating two arrays in Swift

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs += ["Google"]
for item in someStrs {
  print(item)
}

Output:

Apple
Amazon
Google

Above code represents iterate set of values in arrays. Apple, Google, amazon are the set of values . For in syntax is used for iterating these items/displaying these items one by one. Using for loop.

Enumerate arrays in swift

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs += ["Google"]

for (index, item) in someStrs.enumerated() {
 print("Value at index = \(index) is \(item)")
}

Output:

Value at index = 0 is Apple
Value at index = 1 is Amazon
Value at index = 2 is Google

Conclusion:

Hence, we have studied Arrays in swift its syntax and basic operations which can be performed on arrays, creating arrays in swift, Accessing and Printing an Array in Swift, Concatenate two arrays in swift, Iterating two arrays in Swift, Enumerate arrays in swift.

Related Articles:

Swift Input Output with 7 Examples

Swift Variables Strings Array Loop and Functions with Example

iOS Architecture, Concepts and Xcode Installation [4 Steps]

Reference:

swift official page

Geeta Atkar

I am Geeta Atkar PhD(Pursuing), ME (Computer Engg), BE (Information Technology) , Assistant Professor, G H Raisoni College of Engg and Management, Pune and Research Scholar, Vellore Institute of Technology, Chennai

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap