'Is there a way to swap objects of different types?

I am wondering if there is a way to swap two objects of different types in Swift.

Here is my trial:

func swapXY<T>(inout first: T,intout second: T)
{
    (first ,second  ) = ( second,  first)
}

Let's say I want the two parameters to be T,Y respectively. How can this be accomplished?



Solution 1:[1]

Yes you can swap two items, and the function is already included in the standard library.

swap(_:_:)

Exchange the values of a and b.
Declaration

func swap<T>(inout _ a: T, inout _ b: T)

Swift Standard Library Functions Reference

However, if they are not the same type, then no, you cannot swap two items of different types.

Swift 3

func swap<swapType>( _ a: inout swapType, _ b: inout swapType) {
  (a, b) = (b, a)
}

Solution 2:[2]

What you can do is a much more specific swap of classes inheriting from a common ancestor:

class Animal {}
class Dog: Animal {}
class Cat: Animal {}

// Note that cat and dog are both variables of type `Animal`, 
// even though their types are different subclasses of `Animal`.
var cat: Animal = Cat()
var dog: Animal = Dog()

print("cat: \(cat)")
print("dog: \(dog)")

swap(&dog, &cat) // use the standard Swift swap function.

print("After swap:")
print("cat: \(cat)")
print("dog: \(dog)")

The above code works because cat and dog are both "is-a" Animal, both before and after the swap. Swapping objects of unrelated types, however, cannot be done in Swift, nor does it really make sense:

var dog = Dog() // dog is of type Dog, NOT Animal
var cat = Cat() // cat is of type Cat, NOT Animal
swap(&cat, &dog) // Compile error!

This code won't compile because a variable of type Dog cannot hold a value of type Cat in Swift or any other strongly-typed language.

Solution 3:[3]

on first sight it looks terrible, on the second sight ... it could be sometimes very useful function

import Foundation

let a: (Int, String) = (1,"alfa")
let b: (Bool, NSDate) = (true, NSDate())

func foo<A,B>(t: (A,B))->(B,A) {
    return (t.1,t.0)
}

print(a, foo(a))    // (1, "alfa") ("alfa", 1)
print(b, foo(b))    // (true, 2015-11-22 21:50:21 +0000) (2015-11-22 21:50:21 +0000, true)

Solution 4:[4]

var a = 10
var b = 20 
print("A", a)
print("B",b)
a = a + b;//a=30 (10+20)
b = a - b;//b=10 (30-20)
a = a - b;//a=20 (30-10)
print("A",a)
print("B",b)

Solution 5:[5]

use the swapAt() method

provide two indexes inside your array, and the items at those positions will be swapped.

For example, consider this array:

var names = ["Paul", "John", "George", "Ringo"]

If I wanted John and Paul to swap places, I’d swap positions 0 and 1, like this:

names.swapAt(0, 1)

Find the developer documentation here

https://developer.apple.com/documentation/swift/array/2893281-swapat

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eric Aya
Solution 2
Solution 3 user3441734
Solution 4 Ananda Aiwale
Solution 5 TylerH