I am writing my first project in Swift(I have been doing ObjectiveC for years) and for some reason, I cant work out why my enum and Switch are not working togetber
enum ContactViewMode : Int { case ViewModeFavourite = 0 case ViewModeRecent = 1 case ViewModeContacts = 2 }
Property
@IBInspectable var contactViewMode : Int!
Switch
switch contactViewMode { case ContactViewMode.ViewModeFavourite: contacts = DBManager.getFavouriteContacts() as [ContactEntity] case ContactViewMode.ViewModeRecent: contacts = DBManager.getFavouriteContacts() as [ContactEntity] default: contacts = DBManager.getAllContacts() as [ContactEntity] }
The error I get is Enum case 'ViewModeFavourite' is not a member of type 'Int!'
So I changed it to this as it is not a Int! (I need Int! for Storyboard)
var contactMode:Int = contactViewMode switch contactMode { case .ViewModeFavourite: contacts = DBManager.getFavouriteContacts() as [ContactEntity] case .ViewModeRecent: contacts = DBManager.getFavouriteContacts() as [ContactEntity] default: contacts = DBManager.getAllContacts() as [ContactEntity] }
Then I get Enum case pattern cannot match values of the non-enum type 'Int'
Example of Using Enum in Switch Case Statement We will then use the switch case statements to switch between the direction elements and print the output based on the value of the variable for the enum directions.
Enumerations in Swift are much more flexible, and don't have to provide a value for each case of the enumeration. If a value (known as a raw value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.
In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values. We use the enum keyword to create an enum. For example, enum Season { case spring, summer, autumn, winter } Here, Season - name of the enum.
Enumerations (or enums for short) in Swift define a common type for a group of related values. According to the Swift documentation, enums enable you to work with those values in a type-safe way within your code. Enums come in particularly handy when you have a lot of different options you want to encode.
You have to declare contactViewMode
as ContactViewMode
and not Int
.
If you really want it to be Int
, then you have to change the cases in your switch, by comparing the variable to the rawValue
property of the enum cases:
switch contactViewMode { case ContactViewMode.ViewModeFavourite.rawValue: contacts = DBManager.getFavouriteContacts() as [ContactEntity] case ContactViewMode.ViewModeRecent.rawValue: contacts = DBManager.getFavouriteContacts() as [ContactEntity] default: contacts = DBManager.getAllContacts() as [ContactEntity] }
but unless you have a good reason for that, I wouldn't recommend
The other answers fail to take advantage of Swift’s strong typing. Note that they all have a default
case in their switch
. Swift knows at compile time, however, whether you have covered all cases of an enum
or not, thus rendering the default
case unnecessary.
This is a useful feature of the language. Imagine you add an extra case
to your enum
at a later point. The compiler will complain and point you to all the switch
es that are not considering that particular case
.
How can we take advantage of Swift’s strong typing, then?
First, let’s fix the naming of your enumeration. As I assume you are nesting it inside a ContactView
class (and, unlike in Objective-C, it should really be inside it), you can name the enum
simply Mode
. No need to mention ViewMode
s in the cases, either. This way you get a cleaner, more idiomatic type.
enum Mode: Int { case favourite case recent case contacts }
What we need to do is to perform the switch
on a Mode
, not on an Int
. Your Mode
is RawRepresentable
, and as such you can do:
guard let m = Mode(rawValue: contactViewMode) else { /* handle the situation */ } switch m { case .favourite: /* handle case */ case .recent: /* handle case */ case .contacts: /* handle case */ }
That’s all there is to it—instead of switching on the rawValue
, get the proper Mode
first and then switch on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With