Constants in Objective-C

后端 未结 14 1281
广开言路
广开言路 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 22:43

    Easiest way:

    // Prefs.h
    #define PREFS_MY_CONSTANT @"prefs_my_constant"
    

    Better way:

    // Prefs.h
    extern NSString * const PREFS_MY_CONSTANT;
    
    // Prefs.m
    NSString * const PREFS_MY_CONSTANT = @"prefs_my_constant";
    

    One benefit of the second is that changing the value of a constant does not cause a rebuild of your entire program.

提交回复
热议问题