Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Property in Objective C

I have an enum defined in the Constants.h file this way:

typedef enum {
    CalendarTypeMonth = 0,
    CalendarTypeWeek
} CalendarType;

Then in my view controller I determine what the calendar type should be and store it in a property this way:

@property (nonatomic) CalendarType myCalendarType;

Now I want all the classes in my project have access to the calendar type. How can I set this property to be global/extern so that all classes can read this?

EDIT: I know the definition of the enum will be available across the project. But what I am interested in is the value of myCalendarType. How can I access the value of myCalendarType across all classes?

like image 308
NSExplorer Avatar asked Feb 01 '26 23:02

NSExplorer


1 Answers

You can declare class method to access static variable.

Add such code to your implementation file:

static MyStaticType staticVar = MyStaticTypeDefault;

+(BOOL)myStaticVar
{
    return staticVar;
}

+(void)setMyStaticVar:(MyStaticType)newValue
{
    staticVar = newValue;
}

And create declarations for this methods in interface file. This is much better then moving all static values to AppDelegate. Anyway, a lot of variants are possible - for example, you can create singletone to store some settings of application or use CoreData.

like image 108
Nikita Ivaniushchenko Avatar answered Feb 04 '26 15:02

Nikita Ivaniushchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!