How to Swap Array items in Swift? Last Updated : 02 Jun, 2022 Comments Improve Suggest changes Like Article Like Report Swift supports various generic collections like set, dictionary, and array. In Swift, an array is an ordered collection that is used to store the same type of values like string, int, float, etc. In an array, you are not allowed to store the value of a different type in an array, for example, if you have an int type array in this array you are not allowed to store float values, if you try to do so you will get an error. It also keeps duplicate values of the same type. An array can be mutable and immutable. In the Swift array, we can swap the position of the elements. To do so we use the swapAt() function. This function swap two values of the array at the given position. Syntax: arrayName.swapAt(val1, val2) Here, arrayName is the object of the array class. Parameter: This function takes two parameters, i.e., val1 and val2. Here val1 represents the position of the first element to swap and val2 represents the position of the second element to swap. Return Value: It does not return any value. Instead, it swaps the specified elements. Example 1: Swift // Swift program to swap the element of the array import Swift // Creating an array of number // Here the array is of float type var newNumber = [23.034, 1.90, 9.1, 32.34, 560.44, 21.23] print("Array before swapping:", newNumber) // Swapping the element of the array // Using swapAt() function newNumber.swapAt(1, 2) // Displaying the final result print("Array after swapping:", newNumber) Output: Array before swapping: [23.034, 1.9, 9.1, 32.34, 560.44, 21.23] Array after swapping: [23.034, 9.1, 1.9, 32.34, 560.44, 21.23]Example 2: Swift // Swift program to swapping the elements of array import Swift // Creating an array of Emp Name // Here the array is of string type var GfgEmpName = ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"] print("Geeks's employee name before swapping:", GfgEmpName) // Swapping the two elements of the GfgEmpName array // Using swapAt() function GfgEmpName.swapAt(2, 4) // Displaying the final result print("Geeks's employee name after swapping: ", GfgEmpName) Output: Geeks's employee name before swapping: ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"] Geeks's employee name after swapping: ["Sumit", "Poonam", "Mohit", "Bittu", "Punit"] Create Quiz Comment A ankita_saini Follow 0 Improve A ankita_saini Follow 0 Improve Article Tags : Swift Swift-Arrays Swift-Functions Explore Swift Tutorial 10 min read Swift IntroductionSwift - Hello World Program 2 min read Swift - Basic Syntax 5 min read Swift - Keywords 4 min read Swift - Literals 10 min read Swift - Constants, Variables & Print function 2 min read Swift - Operators 8 min read Swift Data TypesSwift - Data Types 5 min read Swift - Integer, Floating-Point Numbers 2 min read Strings in Swift 8 min read String Functions and Operators in Swift 10 min read How to Concatenate Strings in Swift? 3 min read How to Append a String to Another String in Swift? 2 min read How to Insert a Character in String at Specific Index in Swift? 2 min read How to check if a string contains another string in Swift? 2 min read How to remove a specific character from a string in Swift? 2 min read Data Type Conversions in Swift 5 min read Swift - Convert String to Int Swift 3 min read Swift Control FlowSwift - Decision Making Statements 5 min read Swift - If Statement 3 min read Swift - If-else Statement 3 min read Swift - If-else-if Statement 3 min read Swift - Nested if-else Statement 4 min read Swift - Switch Statement 10 min read Swift - Loops 5 min read Swift - While Loop 2 min read Swift - Repeat While loop 3 min read Swift - For-in Loop 6 min read Swift - Control Statements in Loops 5 min read Swift - Break Statement 6 min read Swift - Fallthrough Statement 4 min read Swift - Guard Statement 15 min read Swift FunctionsCalling Functions in Swift 6 min read Swift Function Parameters and Return Values 10 min read How to Use Variadic Parameters in Swift? 6 min read Swift - In Out Parameters 4 min read Swift - Nested Function 5 min read Swift - Function Overloading 8 min read Closures in Swift 9 min read Escaping and Non-Escaping Closures in Swift 5 min read Higher-Order Functions in Swift 10 min read Swift CollectionsSwift - Arrays 9 min read Swift - Arrays Properties 4 min read Swift - How to Store Values in Arrays? 4 min read How to Remove the last element from an Array in Swift? 4 min read How to Remove the First element from an Array in Swift? 4 min read How to count the elements of an Array in Swift? 2 min read How to Reverse an Array in Swift? 3 min read Swift Array joined() function 3 min read How to check if the array contains a given element in Swift? 3 min read Sorting an Array in Swift 5 min read How to Swap Array items in Swift? 2 min read How to check if an array is empty in Swift? 2 min read Swift - Sets 15+ min read Swift - Set Operations 9 min read How to remove first element from the Set in Swift? 2 min read How to remove all the elements from the Set in Swift? 2 min read How to check if the set contains a given element in Swift? 3 min read How to count the elements of a Set in Swift? 2 min read Sorting a Set in Swift 4 min read How to check if a set is empty in Swift? 2 min read How to shuffle the elements of a set in Swift? 2 min read Swift - Difference Between Sets and Arrays 3 min read Swift - Dictionary 9 min read Swift - Tuples 3 min read Swift - Iterate Arrays and Dictionaries 6 min read Swift OOPsSwift Structures 7 min read Swift - Properties and its Different Types 13 min read Swift - Methods 5 min read Swift - Difference Between Function and Method 4 min read Swift - Deinitialization and How its Works? 4 min read Typecasting in Swift 7 min read Repeating Timers in Swift 4 min read Non Repeating Timers in Swift 7 min read Difference between Repeating and Non-Repeating timers in Swift 4 min read Optional Chaining in Swift 9 min read Singleton Class in Swift 4 min read Swift Additional TopicsSwift - Error Handling 2 min read Difference between Try, Try?, and Try! in Swift 4 min read Swift - Typealias 7 min read Important Points to Know About Java and Swift 3 min read Difference between Swift Structures and C Structure 4 min read How to Build and Publish SCADE Apps to Apple and Google Stores? 11 min read 6 Best iOS Project Ideas For Beginners 8 min read Like