Thursday, March 22, 2012

How to use enum in Objective C ?

Ok so this post is about how to use enum. Enumerations are very useful when it creating a type which will take only predefined values. In order to avoid having to use the enum keyword everywhere, a typedef can be created:

typedef enum
    mLineType = 10,

 Now you know how to create enum, how do you use it though ? You create a class say MyChart and have ChartType as one of the member variable.

 @interface MyChart : UIView

    NSArray *valueArray;
    ChartType type;

    int levels_;
    float yOffset;
    float xOffset;

 @property(nonatomic,retain)NSArray *valueArray;
 @property(nonatomic,assign) ChartType type;


Now you have create an instance of MyChart and define its type as following.

 MyChart *chart =[[MyChart alloc]initWithFrame:a];
 [chart drawGraph];
 [self.view addSubview:chart];

Now there is this unique problems in enums you may come across sometime that what if 2 enums ended up having some values same, your code will show ERROR, how to solve this ? See you in NEXT ARTICLE.

1 comment:

  1. nice blogs
    Sports Packsms ACT SPO and send it to 50000. And to deactivate the service sms DEACT SPO and send it to 50000
    Rs. 7/Week.