You are reading the article Learn How To Declare And Initialize An Array In Swift? updated in September 2023 on the website Khongconthamnam.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn How To Declare And Initialize An Array In Swift?
Introduction to Swift arraySwift Array is the data structure that acts as the container for storing some values of the same type in an ordered list. Swift programming language does not allow much flexibility in terms of accessing the elements and manipulation rather follows quite a strict restriction in terms of Arrays declaration and manipulation. Various operations like adding, removing, or changing values are allowed in the Swift array, but invalid values may create some intrusion which needs to be handled carefully. Assigning a constant value in Swift array might create an immutable array, and contents cannot get changed.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
Various operations can be performed over a swift array, some of them with the syntax is described below:
The syntax for Creating an Empty array containing no value with the initializer is represented as,
var sm_arr = [sm_data] ( )sm_data represents the empty array initialization.
The syntax for Accessing the elements from the swift array is represented as follows:
var sm_vr = Sm_Array[index]where Sm_Array is used for representing the array, and index is used for accessing the elements.
How to declare an array in Swift?Declaring an array in Swift is quite easy, and it then allows all the elements and values to get accessed and manipulated easily. There are two ways to declare an array which are as follows:
One way is to initialize the variable with an empty array.
Another way is to use the automatic type inference.
# To initialize the variable with an empty array:
The syntax is like the representation in the syntax section and is used accordingly.
# To use the automatic type inference.
How to initialize an array in Swift?Initialization of array in Swift is useful for performing operations and manipulations on the elements or items present in the Swift array concerning creating, accessing, and modifying. Also, it performs all the operations on top of it. Fetching of the index and returning the index required, while traversal in the index within the range, also plays an important role.
It is initialized in the following manner:
var arr_Title = [String]()where arr_Title is the array variable, and [String] () is the array declaration where initialization with the string will be performed.
Note: Since a dynamic array is being used as part of initialization, it must append some value.
For Example:
var arr_Title = [String]() arr_Title.append("strng_text") print(arr_Title[1]);A very important point to keep in mind is that Array initialization in the swift language is always performed after the array declaration. Since the declaration is the first step, otherwise initialization will be incomplete.
ExamplesHere are the following examples mention below
Example #1This program demonstrates the initialization of an empty array and printing an empty array as it is not assigned any value as shown in the output.
import Foundation import Glibc let emp_Int_Arr:[Int] = [] print(emp_Int_Arr)Output:
Example #2This program demonstrates the usage of string array by appending and adding some of the vegetables in the array and then performed a count on top of it as shown in the output.
import Foundation import Glibc var veggies = [String]() veggies.append("Onion") veggies.append("Potato") veggies.append("Radish") print(veggies.count) print(veggies)Output:
Example #3This program demonstrates the string array of fruits which demonstrates the accessibility of fruits concerning the first and the last element index as shown in the output where the first index element is kiwi, and the last one is orange.
import Foundation import Glibc let fruits: [String] = ["kiwi", "apple", "orange"] print(fruits[0]) print(fruits[fruits.count - 1])Output:
Example #4This program demonstrates the string array in swift, which is used for displaying the usage of enumerated function as shown in the output below.
import Foundation import Glibc var cars = ["bmw", "lamborgini", "jaguar"] for (indx, val) in cars.enumerated() { print("(indx) = (val)") }Output:
Example #5This program demonstrates the usage of array initializer, which is used for counting the number of elements in an array with respective sizes as shown in the output below.
import Foundation import Glibc var size_1: [Int] = [8, 6, 12,18,22] size_1.append(26) print("size_1: (size_1)")Output:
Example #6This program demonstrates the usage of the Int array by using a for-in loop to get the unique number that is being appended in the swift array, as shown in the output below.
import Foundation import Glibc var uniq_no = [Int]() uniq_no.append(15) uniq_no.append(13) uniq_no.append(20) for uniq_no in uniq_no { print(uniq_no) }Output:
Example #7This program demonstrates the usage of subscript syntax to check for the accessing of elements in Swift to make the index lie with the runtime check.
import Foundation import Glibc let int_arr_0 = [20, 18, 16, 23] print(int_arr_0 [-1])Output:
Example #8This program demonstrates the swift array to be present in the reverse order and makes the elements represented similarly as shown in the output below as per the indexing.
Note: Basically, the array returns elements of an array in reverse order.
import Foundation import Glibc var int_ar_2 = [20,15,2,18] let revrs_arr = Array(int_ar_2.reversed()) print(revrs_arr)Output:
ConclusionSwift Array plays a pivotal role in a swift programming language as it allows programmers to work according to the requirement to keep the elements in place and ordered. Many operations like creation, addition, deletion, removal, and many operations can be performed on top of it for manipulating the data structure array properly.
Recommended ArticlesWe hope that this EDUCBA information on “Swift array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Learn How To Declare And Initialize An Array In Swift?
Update the detailed information about Learn How To Declare And Initialize An Array In Swift? on the Khongconthamnam.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!