Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Cast Int into enum:Int

Tags:

xcode

ios

swift

People also ask

Can you cast an int to an enum?

You can explicitly type cast an int to a particular enum type, as shown below.

How do I assign an integer to an enum?

Use the Type Casting to Convert an Int to Enum in C# The correct syntax to use type casting is as follows. Copy YourEnum variableName = (YourEnum)yourInt; The program below shows how we can use the type casting to cast an int to enum in C#. We have cast our integer value to enum constant One .

Can enum inherit Swift?

In Swift language, we have Structs, Enum and Classes. Struct and Enum are passed by copy but Classes are passed by reference. Only Classes support inheritance, Enum and Struct don't.

What is raw value in enum Swift?

Each raw value for our enum case must be a unique string, character, or value of any integer or floating-point type. This means the value for the two case statements cannot be the same.


Use the rawValue initializer: it's an initializer automatically generated for enums.

self.timeFilterSelected = MyTimeFilter(rawValue: (sender as UIButton).tag)!

see: The Swift Programming Language § Enumerations


NOTE: This answer has changed. Earlier version of Swift use the class method fromRaw() to convert raw values to enumerated values.