Cody Weaver Cody Weaver - 5 months ago 9
Swift Question

What protocols do all numbers conform to in Swift?

I am trying to figure out what protocols in Swift make it so I have a function that only takes Numbers. I want to be able to pass

Int
and
Double
,
Float
and
Float
, pretty much any combination of Numbers.

func takeNumbers(A: Protocol1, Protocol2, B: Protocol1, Protocol2) {

}


I just listed two protocols I don't know if it I need 1,2,3, etc.

Answer

Why not create your own protocol and extend each type to conform to it?

Further, you could use a protocol extension to implement a common method to get the value of each. For instance, you could return an int value from each by implementing switch logic on the type.

Also, you said Float twice.

Comments