Where do you declare a constant in Objective-C?

后端 未结 4 1607
故里飘歌
故里飘歌 2020-12-07 18:54

I declared a constant in a header file const double EARTH_RADIUS=6353; that is imported to various other headers and I got a linker error.

L         


        
4条回答
  •  独厮守ぢ
    2020-12-07 19:09

    There are two ways to accomplish this:

    1st option- As previous replies pointed, in the .h file:

    myfile.h
    extern const int MY_CONSTANT_VARIABLE;
    

    and in myfile.m define them

    myfile.m    
    const int MY_CONSTANT_VARIABLE = 5;
    

    2nd option- My favourite:

    myfile.h
    static const int MY_CONSTANT_VARIABLE = 5 ;
    

提交回复
热议问题