EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Swift Tutorial Swift array

Swift array

Updated April 6, 2023

Swift array

Introduction to Swift array

Swift 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] ( )

where, sm_arr is the variable for storing the value and 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.

For Example, >  var m_var: Array<M_Class> = []

# To use the automatic type inference.

For Example, > var m_var = Array<M_Class>()

The basic advantage of this type of declaration can also be written as Array<M_Class> as [M_Class]

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.

Examples

Here are the following examples mention below

Example #1

This 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:

Swift array output 1

Example #2

This 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:

Swift array output 2

Example #3

This 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:

Swift array output 3

Example #4

This 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:

output 4

Example #5

This 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:

output 5

Example #6

This 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:

output 6

Example #7

This 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:

output 7

Example #8

This 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:

output 8

Conclusion

Swift 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 Articles

We hope that this EDUCBA information on “Swift array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Swift For Loop
  2. Swift Version
  3. What is Swift?
  4. Swift Operators
PROGRAMMING LANGUAGES Course Bundle - 54 Courses in 1 | 4 Mock Tests
338+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Course Bundle - 15 Courses in 1 | 9 Mock Tests
39+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System Course Bundle - 7 Courses in 1
43+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Course Bundle - 6 Courses in 1
15+ Hour of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
SWIFT Course Bundle - 21 Courses in 1
 56+ Hours of HD Videos
21 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more