Constants in Objective-C and “duplicate symbol” linker error

后端 未结 1 879
死守一世寂寞
死守一世寂寞 2021-01-04 09:46

I\'ve declared a constant with the same name in some different classes, in their .m file, this way:

@implementation MyViewController
const NSInt         


        
相关标签:
1条回答
  • 2021-01-04 10:43

    If you want to use constant only in one .m file then declare it as static. For example:static NSString * const CONSTANT_STRING = @"Constant I am".

    In case of NSInteger you can write in your every .m file:

    static const NSInteger my_const = 3;
    

    If you want globals (one constant with one value visible in every file) then write in your .h:

    extern const NSInteger my_global_const;
    

    and in your .m file you can add

    const NSInteger my_global_const = 5;
    
    0 讨论(0)
提交回复
热议问题