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 protected])
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]