MobileEntityLogo.png

Hi.

Welcome to the best online resource for IOS & Android tutorials.

An in depth look at generics in Swift

An in depth look at generics in Swift

What is generics

Say you want to create a function that adds two numbers, however you want to accept integers, doubles, and floating point numbers. In some languages you have to write this function three times, one for each data type. In swift their is no need for this as you can use generics to create one function that accepts any numeric type. This expands to many other branches of code as you will see shortly.


func add<T:Numeric>(a:T,b:T) -> T {
    a+b
}

The function above adds two numbers and returns the result. The T represents a generic type. you define this using <T> before the function parameters as shown above. Our function says <T:Numeric> because we are creating a generic that must be numeric in order to be able to perform math with the generics. Our function returns our generic T which means it can return any numeric type. We return a+b which returns our calculation. NOTE; The keyword return is omitted because we are using implicit returns since the only code in our function is what we need to be returned.


print(Int(add(a: 4, b: 5)))
print(Double(add(a: 4.5, b: 5.4)))

Above is an example of how to use our add function. As you see we print two calculations. One using decimal values, and one using integer values.

Let’s dive a little bit deeper

Generics aren’t limited to functions only, their are many places to use them including classes, and structs.


class SpaceShip<T> {
    func launch(kind:T) {
        print(kind)
    }
}

struct SpaceShip<T> {
    func launch(kind:T) {
        print(kind)
    }
}

As you see generics can be gracefully added to any class or struct, and most parts of swift code. Generics have protocols it needs to conform to such as numeric for number operations, or comparable for comparing two pieces of code, there are many protocols you can use in generics to create very custom and reusable functionality. I hope this article is useful to you. If you have any questions please comment, and I will do my best to answer them. Below is a few examples of generics to take a look at.


func add<T:Numeric>(a:T,b:T) -> T {
    a+b
}
print(Int(add(a: 4, b: 5)))
print(Double(add(a: 4.5, b: 5.4)))

func devide<T:FloatingPoint>(a:T,b:T) -> T {
    a/b
}
print(devide(a: 10, b: 2))
print(devide(a: 10.14159, b: 2.47372))
func compare<T:Comparable>(a:T,b:T) -> Bool {
    a == b
}
print(compare(a: 1, b: 1))
print(compare(a: 5, b: 1))
print(compare(a: "Bob", b: "Bob"))
print(compare(a: "Bob", b: "Bab"))

class SpaceShip<T> {
    func launch(kind:T) {
        print(kind)
    }
}
SpaceShip().launch(kind: "Falcon 9")
SpaceShip().launch(kind: 550.5)
SpaceShip().launch(kind: 1031)

struct Space<T> {
    func Planet(atmosphere:T) {
        print(atmosphere)
    }
}
class Atmosphere:NSObject {
    init(pressure:Int,axygen:Int) {
        //Do stuff
    }
}
Space().Planet(atmosphere: Atmosphere(pressure: 50, axygen: 10))
Space().Planet(atmosphere: 50.5)
Space().Planet(atmosphere: 20)
Space().Planet(atmosphere: "Thin")
How to fix NDEF is disallowed error when uploading NFC enabled app to app store connect

How to fix NDEF is disallowed error when uploading NFC enabled app to app store connect

Top 20 most asked IOS interview questions and answers

0