Constants in Objective-C

后端 未结 14 1327
广开言路
广开言路 2020-11-21 22:10

I\'m developing a Cocoa application, and I\'m using constant NSStrings as ways to store key names for my preferences.

I understand this is a good idea b

14条回答
  •  猫巷女王i
    2020-11-21 22:48

    There is also one thing to mention. If you need a non global constant, you should use static keyword.

    Example

    // In your *.m file
    static NSString * const kNSStringConst = @"const value";
    

    Because of the static keyword, this const is not visible outside of the file.


    Minor correction by @QuinnTaylor: static variables are visible within a compilation unit. Usually, this is a single .m file (as in this example), but it can bite you if you declare it in a header which is included elsewhere, since you'll get linker errors after compilation

提交回复
热议问题