Where do I have to declare static variables?

后端 未结 3 816
臣服心动
臣服心动 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:41

    Globals can go pretty much wherever you want; just put it in whatever place makes sense stylistically. I prefer to see globals near the top of source files, personally.

    While you could put the definition into a header file, I don't recommend it. Putting any kind of definition in a header file can lead to multiply-defined symbol linker errors on down the road. If you need more than one compilation unit to see the variable, you can't make it static anyway - you'll need to define it in an implementation file somewhere and use extern to make it visible amongst various source files.

提交回复
热议问题