Swift Tutorial

Swift Data Types

Swift Control Flow

Swift Functions

Swift Collections

Swift OOPs

Swift Additional Topics

How to Swap Array items in Swift?

In Swift, if you want to swap two items in an array at specific indices, you can use the swapAt(_:_:) method provided by the Array type.

Here's how you can do it:

var numbers = [1, 2, 3, 4, 5]

// Swap the items at index 1 and 3 (swap 2 with 4)
numbers.swapAt(1, 3)

print(numbers)  // [1, 4, 3, 2, 5]

In this example, we've swapped the number 2 at index 1 with the number 4 at index 3.

Ensure the indices provided are within the range of the array's indices; otherwise, a runtime crash will occur.

  1. Swift swap array elements:

    • Swapping elements in a Swift array can be achieved using the swapAt(_:_:) method.
    • Example:
      var numbers = [1, 2, 3, 4, 5]
      numbers.swapAt(1, 3)
      print(numbers) // Output: [1, 4, 3, 2, 5]
      
  2. How to exchange elements in Swift array:

    • Exchanging elements in a Swift array is synonymous with swapping, and it's done using the swapAt(_:_:) method.
    • Example:
      var letters = ["A", "B", "C", "D"]
      letters.swapAt(0, 2)
      print(letters) // Output: ["C", "B", "A", "D"]
      
  3. Swift array swap method:

    • The swapAt(_:_:) method is the primary method for swapping elements in a Swift array.
    • Example:
      var fruits = ["Apple", "Banana", "Orange"]
      fruits.swapAt(0, 2)
      print(fruits) // Output: ["Orange", "Banana", "Apple"]
      
  4. Swapping array elements in Swift example:

    • Swapping array elements in Swift is done using the swapAt(_:_:) method, which takes the indices of the elements to be swapped.
    • Example:
      var names = ["Alice", "Bob", "Charlie"]
      names.swapAt(0, 2)
      print(names) // Output: ["Charlie", "Bob", "Alice"]
      
  5. Reorder array items in Swift:

    • Reordering array items in Swift is synonymous with swapping or changing the positions of elements.
    • Example:
      var colors = ["Red", "Green", "Blue"]
      colors.swapAt(1, 2)
      print(colors) // Output: ["Red", "Blue", "Green"]
      
  6. Change position of array elements in Swift:

    • Changing the position of array elements in Swift can be achieved using the swapAt(_:_:) method.
    • Example:
      var animals = ["Dog", "Cat", "Bird"]
      animals.swapAt(0, 1)
      print(animals) // Output: ["Cat", "Dog", "Bird"]
      
  7. Swift array exchange positions:

    • Exchanging positions of elements in a Swift array is synonymous with swapping, and it's done using the swapAt(_:_:) method.
    • Example:
      var cities = ["New York", "Los Angeles", "Chicago"]
      cities.swapAt(1, 2)
      print(cities) // Output: ["New York", "Chicago", "Los Angeles"]
      
  8. Swap two elements in array Swift:

    • Swapping two elements in a Swift array is done using the swapAt(_:_:) method.
    • Example:
      var items = ["Item1", "Item2", "Item3"]
      items.swapAt(0, 1)
      print(items) // Output: ["Item2", "Item1", "Item3"]
      
  9. Shuffle and swap array items in Swift:

    • Shuffling and swapping array items are distinct operations. Shuffling involves randomizing the order of elements, while swapping involves exchanging specific elements.
    • Example (Shuffling):
      var numbers = [1, 2, 3, 4, 5]
      numbers.shuffle()
      print(numbers) // Output: Randomized order of elements