Global Property in Objective C

后端 未结 2 652
臣服心动
臣服心动 2021-01-14 08:52

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

typedef enum {
    CalendarTypeMonth = 0,
    CalendarTypeWeek
} CalendarType;

The

相关标签:
2条回答
  • 2021-01-14 09:46

    In your [Project Name]_Prefix.pch file, you can add this statement:

    #import "Constants.h"
    

    In so doing, you'll make your enum available to every file in the project.

    EDIT: To access the value, I suggest making it a global property of your application delegate. To take it a step further, you can make your application delegate a method of NSObject using a category so it might look something like:

    CalendarType current = [self appDelegate].currentCalendarType
    

    Observe that your property is now a globally gettable/settable property.

    0 讨论(0)
  • 2021-01-14 09:53

    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.

    0 讨论(0)
提交回复
热议问题