Tuesday, July 17, 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,
    mBarType,
    mStackedType
}ChartType;


 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;


 @private
    int levels_;
    float yOffset;
    float xOffset;
 }

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

 -(void)drawGraph;
 @end



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


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






No comments:

Post a Comment