Jared
Jared

Reputation: 953

Swift 3 custom parameter types

I'm sorry if this has been asked before, I can't find an answer. I would like to create custom parameter types for a function.

Typedef?/Define type

direction
{
    LeftToRight,
    RightToLeft
};

Function:

class func animateIn (dir:direction)
{
    if dir = LeftToRight
    {
        // animate left to right
    }
    else
    {
        // animate right to left
    }
}

Call:

animateIn (dir:LeftToRight)

Upvotes: 0

Views: 40

Answers (2)

Vini App
Vini App

Reputation: 7485

enum Direction
{
    case leftToRight, rightToLeft
}

Function:

class func animateIn(dir:Direction)
{
    switch dir {
    case .leftToRight:
        // animate left to right
    default:
        // animate right to left    
    }
}

Call:

animateIn(dir:.leftToRight)

Upvotes: 1

David Pasztor
David Pasztor

Reputation: 54706

enum seems the perfect candidate for this use. If you plan to have more cases in the enum, a switch statement also seems to be more feasible inside the function.

enum Direction {
    case leftToRight, rightToLeft
}

class func animateIn(dir: Direction){
    switch dir{
    case .leftToRight:
        //do something
    case .rightToLeft:
        //do something
    }
}

Upvotes: 2

Related Questions