• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Swift function overloading - invoking func with specific parameter (in place of Any parameter) on passing Any object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Swift function overloading - invoking func with specific parameter (in place of Any parameter) on passing Any object


  • Subject: Swift function overloading - invoking func with specific parameter (in place of Any parameter) on passing Any object
  • From: Devarshi Kulshreshtha via Cocoa-dev <email@hidden>
  • Date: Sun, 27 Oct 2019 13:43:41 +0530

I have 3 structs:

    struct Address: Codable {
        var addressLine1: String
    }
    struct Person: Codable {
        var name: String
    }
    struct Order: Codable {
        var person: Person?
        var address: Address?
    }

In my ViewController class I am using Mirror to access each of the
properties within Order:

    let address = Address(addressLine1: "Some unknown address")
    let person = Person(name: "Some unknown name")
    let order = Order(person: person, address: address)

    let newOrderMirror = Mirror(reflecting: order)
    newOrderMirror.children.forEach {
        display(child: $0.value)
    }

In ViewController I have implemented 3 display functions:

    func display(child: Any) {
        // this should never get called
        print(child)
    }
    func display(child: Person) {
        print(child)
    }
    func display(child: Address) {
        print(child)
    }

In above case it is always invoking `func display(child: Any)`, however I
want it to invoke the functions with specific parameter. Is there any way
to achieve this without type casting?:

Off-course this will work:

        newOrderMirror.children.forEach {
            if let bs = $0.value as? Person {
                display(child: bs)
            } else if let addr = $0.value as? Address {
                display(child: addr)
            }
            display(child: $0.value)
        }

Is there any other elegant way to achieve the desired behavior without
using `if-let + typecasting`?

--
Thanks,

Devarshi
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Swift function overloading - invoking func with specific parameter (in place of Any parameter) on passing Any object
      • From: Andrew Thompson via Cocoa-dev <email@hidden>
  • Prev by Date: Re: Thoughts on productivity
  • Next by Date: Re: Swift function overloading - invoking func with specific parameter (in place of Any parameter) on passing Any object
  • Previous by thread: Re: Fullscreen window problems
  • Next by thread: Re: Swift function overloading - invoking func with specific parameter (in place of Any parameter) on passing Any object
  • Index(es):
    • Date
    • Thread