Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 180 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is a typedef enum in Objective-C?

#1
I don't think I fundamentally understand what an `enum` is, and when to use it.

For example:

typedef enum {
kCircle,
kRectangle,
kOblateSpheroid
} ShapeType;

What is really being declared here?
Reply

#2
Three things are being declared here: an anonymous enumerated type is declared, `ShapeType` is being declared a typedef for that anonymous enumeration, and the three names `kCircle`, `kRectangle`, and `kOblateSpheroid` are being declared as integral constants.

Let's break that down. In the simplest case, an enumeration can be declared as

enum tagname { ... };

This declares an enumeration with the tag `tagname`. In C and Objective-C (but *not* C++), any references to this *must* be preceded with the `enum` keyword. For example:

enum tagname x; // declare x of type 'enum tagname'
tagname x; // ERROR in C/Objective-C, OK in C++

In order to avoid having to use the `enum` keyword everywhere, a typedef can be created:

enum tagname { ... };
typedef enum tagname tagname; // declare 'tagname' as a typedef for 'enum tagname'

This can be simplified into one line:

typedef enum tagname { ... } tagname; // declare both 'enum tagname' and 'tagname'

And finally, if we don't need to be able to use `enum tagname` with the `enum` keyword, we can make the `enum` anonymous and only declare it with the typedef name:

typedef enum { ... } tagname;

Now, in this case, we're declaring `ShapeType` to be a typedef'ed name of an anonymous enumeration. `ShapeType` is really just an integral type, and should only be used to declare variables which hold one of the values listed in the declaration (that is, one of `kCircle`, `kRectangle`, and `kOblateSpheroid`). You can assign a `ShapeType` variable another value by casting, though, so you have to be careful when reading enum values.

Finally, `kCircle`, `kRectangle`, and `kOblateSpheroid` are declared as integral constants in the global namespace. Since no specific values were specified, they get assigned to consecutive integers starting with 0, so `kCircle` is 0, `kRectangle` is 1, and `kOblateSpheroid` is 2.
Reply

#3
A user defined type that has the possible values of `kCircle`, `kRectangle`, or `kOblateSpheroid`. The values inside the enum (kCircle, etc) are visible outside the enum, though. It's important to keep that in mind (`int i = kCircle;` is valid, for example).

Reply

#4
`typedef` is useful for redefining the name of an existing variable type. It provides short & meaningful way to call a datatype.
e.g:

typedef unsigned long int TWOWORDS;

here, the type unsigned long int is redefined to be of the type TWOWORDS. Thus, we can now declare variables of type unsigned long int by writing,

TWOWORDS var1, var2;

instead of

unsigned long int var1, var2;
Reply

#5
enum is used to assign value to enum elements which cannot be done in struct. So everytime instead of accessing the complete variable we can do it by the value we assign to the variables in enum. By default it starts with 0 assignment but we can assign it any value and the next variable in enum will be assigned a value the previous value +1.
Reply

#6
A typedef allows the programmer to define one Objective-C type as another. For example,

typedef int Counter; defines the type Counter to be equivalent to the int type. This drastically improves code readability.
Reply

#7
The Typedef is a Keyword in C and C++. It is used to create new names for basic data types **(char, int, float, double, struct & enum)**.

typedef enum {
kCircle,
kRectangle,
kOblateSpheroid
} ShapeType;

Here it creates enumerated data type **ShapeType** & we can write new names for enum type **ShapeType** as given below

ShapeType shape1;
ShapeType shape2;
ShapeType shape3;

Reply

#8
typedef enum {
kCircle,
kRectangle,
kOblateSpheroid
} ShapeType;


then you can use it like :-

ShapeType shape;
and

enum {
kCircle,
kRectangle,
kOblateSpheroid
}
ShapeType;

now you can use it like:-

enum ShapeType shape;
Reply

#9
You can use in the below format, Raw default value starting from 0, so

- kCircle is 0,
- kRectangle is 1,
- kOblateSpheroid is 2.

You can assign your own specific start value.

typedef enum : NSUInteger {
kCircle, // for your value; kCircle = 5, ...
kRectangle,
kOblateSpheroid
} ShapeType;

ShapeType circleShape = kCircle;
NSLog(@"%lu", (unsigned long) circleShape); // prints: 0
Reply

#10
enum can reduce many types of "errors" and make the code more manageable

#define STATE_GOOD 0
#define STATE_BAD 1
#define STATE_OTHER 2
int STATE = STATE_OTHER

The definition has no constraints. It's simply just a substitution.
It is not able to limit all conditions of the state. When the STATE is assigned to 5, the program will be wrong, because there is no matching state. But the compiler is not going to warn STATE = 5

So it is better to use like this

typedef enum SampleState {
SampleStateGood = 0,
SampleStateBad,
SampleStateOther
} SampleState;

SampleState state = SampleStateGood;
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through