Where do I have to declare static variables?

后端 未结 3 807
臣服心动
臣服心动 2021-01-02 16:09

i.e. I want to bring this in my code:

static BOOL MyConstantBool = YES;

Must it be before or after @implementation? Are there rules where t

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-02 16:28

    If it's after the @implementation block, then you can't use it in the @implementation block (unless it's been forward declared somewhere else using extern). Here's how I do it:

    //Constants.h
    extern BOOL MyConstantBool;
    extern NSString* MyConstantString;
    
    
    //Constants.m
    #import "Constants.h"
    BOOL MyConstantBool = YES;
    NSString* MyConstantString = @"Hello, world!";
    
    
    //SomeOtherFile.m
    #import "Constants.h" 
    //you can now use anything declared in Constants.h 
    

提交回复
热议问题