Objective-C static, extern, public variables

后端 未结 5 2199
悲哀的现实
悲哀的现实 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条回答
  •  Happy的楠姐
    2020-12-12 12:47

    Separate global variable (one per source file):

    // .h
    static NSString * aStatic;
    
    //.m
    static NSString * aStatic = @"separate";
    

    Unique global variable:

    // .h
    extern NSString * anExtern;
    
    // .m
    NSString * anExtern = @"global";
    

提交回复
热议问题