EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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 Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW