Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I allow my function to accepts two different objects in swift?

I have a function which takes one argument. I wanted my function to accept two object types. How can I do it? Here is the example below:

func accept(user: Customer) {
  ...
}

It should accept Customer and Employee object reference.

accept(objRefCustomer)
accept(objRefEmployee)

Please help me in this case.

like image 545
coolly Avatar asked May 03 '16 05:05

coolly


People also ask

How do you pass a function to a different function in Swift?

To pass function as parameter to another function in Swift, declare the parameter to receive a function with specific parameters and return type. The syntax to declare the parameter that can accept a function is same as that of declaring a variable to store a function.

What is a Variadic function Swift?

In computer programming, a variadic function is a function which accepts a variable number of arguments. The function arguments are represented by … (three period characters) after the argument's type that can be accessed into their body as an array .

Does function have types in Swift?

Every function in Swift has a type, consisting of the function's parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions.

What is #function in Swift?

Function Definition In Swift 4, a function is defined by the "func" keyword. When a function is newly defined, it may take one or several values as input 'parameters' to the function and it will process the functions in the main body and pass back the values to the functions as output 'return types'.


1 Answers

Alternative to super-classing: use protocols

You needn't necessarily use a superclass for this case (if Customer and Employee are struct value types; superclass option is not possible), but can rather use the more generic approach of protocols.

Define a protocol Users which blueprints properties and methods for your Customer and Employee instances (if we let Customer and Employee conform to Users, then we promise that instances of these two structures will have accessible the blueprinted properties and methods):

protocol Users {
    var name: String { get }
    func printTypeOfUser()
}

Define the Customer and Employee structures, and their conformance to the protocol Users:

struct Customer : Users {
    let name: String
    init(name: String) { self.name = name }

    func printTypeOfUser() {
        print("Is a Customer!")
    }
}

struct Employee : Users {
    let name: String
    let id: Int
    init(name: String, id: Int) { self.name = name; self.id = id }
    
    func printTypeOfUser() {
        print("Is an Employee!")
    }
}

Now you can define a generic function where its generic, say T, is type constrained to types conforming to the protocol Users, which in this case is equivalent to the Customer or Employee types

func accept<T: Users>(user: T) {
    print("Name of user: \(user.name) [\(user.dynamicType)]")
    user.printTypeOfUser()
    
    // do something additional employee-specific if user is an employee?
    if let employee = user as? Employee {
        print("User is an employee with id: \(employee.id)")
    }
}

Example usage of this function for Employee as well as Customer instances:

let employee = Employee(name: "John", id: 1)
let customer = Customer(name: "Sarah")

accept(employee) /* Name of user: John [Employee] 
                    Is an Employee!
                    User is an employee with id: 1 */

accept(customer) /* Name of user: Sarah [Customer]
                    Is a Customer!                 */
like image 152
dfrib Avatar answered Sep 19 '22 15:09

dfrib