Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to create and how to use Enum in iOS?

I have started learning iOS development.

I want to use enum in my sample project.

I've declared enum in sample.h like following. I hope I've declared this correctly.

typedef enum{s=1,m,t,w,th,f,sa} days; 

I want to use this in viewController.m. In viewController.h, I've imported sample.h.

I want to use enum with the name like "days.sa". But more code i searched in google, they've said like create a instance variable in "sample.h" like

@interface Sample:NSObject {     days d; } 

If I want to use this means, I need to create and use instance. But I don't want like that.

I need to use like

days.d or days.sa or days.th 

How to do that ?, This must be used for the whole Project and

How to create enum as class variable instead of instance variable ?

like image 253
Jeeva J Avatar asked Mar 05 '14 10:03

Jeeva J


People also ask

Where do you declare enum?

The best way to define the enum is to declare it in header file. So, that you can use it anywhere you want by including that header file during compilation.

Where do we use enum?

You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.

What is enum IOS?

An enumeration is a data type consisting of a set of named values, called members. Apple doc says: An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

What is the use of enum in Swift?

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.


2 Answers

In the enum you've created, s, m etc. are now available globally (i.e. to anything that imports sample.h). If you want the integer corresponding to Saturday, for example, it's just sa, not days.sa. I think you're confusing enums with structures.

For this reason, it's better to use more verbose names in your enum. Something like:

typedef enum {     WeekdaySunday = 1,     WeekdayMonday,     WeekdayTuesday,     WeekdayWednesday,     WeekdayThursday,     WeekdayFriday,     WeekdaySaturday } Weekday; 

so e.g. WeekdayMonday is now just another way of writing 2 in your app, but will make your code more readable and pre-defines the possible legal values for a variable of type Weekday.

The above is fine, but for better compiler support and to ensure the size of a Weekday, I'd recommend using NS_ENUM:

typedef NS_ENUM(NSInteger, Weekday) {     WeekdaySunday = 1,     WeekdayMonday,     WeekdayTuesday,     WeekdayWednesday,     WeekdayThursday,     WeekdayFriday,     WeekdaySaturday }; 
like image 187
stefandouganhyde Avatar answered Oct 04 '22 01:10

stefandouganhyde


hey you use enum like this here is an example

In .h define enum

typedef enum{s=1,m,t,w,th,f,sa} days; 

In .m play with enum element like this

days d1 =f;        switch (d1) {         case m:         case t:             NSLog(@"You like Tuesday");             break;         case w:         case th:              break;         case f:             NSLog(@"You like friday");             break;         case sa:             NSLog(@"You satureday");             break;         case s:             NSLog(@"You like sunday");             break;         default:             break;     } 

if you want learn more click this.

like image 28
Gajendra Rawat Avatar answered Oct 04 '22 01:10

Gajendra Rawat