Constants in Objective-C

后端 未结 14 1323
广开言路
广开言路 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条回答
  •  臣服心动
    2020-11-21 23:00

    I myself have a header dedicated to declaring constant NSStrings used for preferences like so:

    extern NSString * const PPRememberMusicList;
    extern NSString * const PPLoadMusicAtListLoad;
    extern NSString * const PPAfterPlayingMusic;
    extern NSString * const PPGotoStartupAfterPlaying;
    

    Then declaring them in the accompanying .m file:

    NSString * const PPRememberMusicList = @"Remember Music List";
    NSString * const PPLoadMusicAtListLoad = @"Load music when loading list";
    NSString * const PPAfterPlayingMusic = @"After playing music";
    NSString * const PPGotoStartupAfterPlaying = @"Go to startup pos. after playing";
    

    This approach has served me well.

    Edit: Note that this works best if the strings are used in multiple files. If only one file uses it, you can just do #define kNSStringConstant @"Constant NSString" in the .m file that uses the string.

提交回复
热议问题