Objective-C static, extern, public variables

后端 未结 5 2204
悲哀的现实
悲哀的现实 2020-12-12 12:14

I want to have a variable that I can access anywhere by importing a header file but I also want it to be static in the sense that there is only one of them created. In my .

5条回答
  •  误落风尘
    2020-12-12 12:39

    I normally use this layout for my statics:

    NSMutableArray *macroArray;
    BOOL keepMacro;
    
    + (void) startMacro
    {
        if (macroArray == nil)
        {
            macroArray = [[NSMutableArray alloc] initWithCapacity:100];
        }
    
        [macroArray removeAllObjects];
        keepMacro = YES;
    }
    

    This is the startMacro command in my application. Both the Bool and the macroArray are static, but notice they are not declared static or extern.

    This may not be the best practice, but this is what I do.

提交回复
热议问题