Constants in Objective-C

后端 未结 14 1280
广开言路
广开言路 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:39

    I am generally using the way posted by Barry Wark and Rahul Gupta.

    Although, I do not like repeating the same words in both .h and .m file. Note, that in the following example the line is almost identical in both files:

    // file.h
    extern NSString* const MyConst;
    
    //file.m
    NSString* const MyConst = @"Lorem ipsum";
    

    Therefore, what I like to do is to use some C preprocessor machinery. Let me explain through the example.

    I have a header file which defines the macro STR_CONST(name, value):

    // StringConsts.h
    #ifdef SYNTHESIZE_CONSTS
    # define STR_CONST(name, value) NSString* const name = @ value
    #else
    # define STR_CONST(name, value) extern NSString* const name
    #endif
    

    The in my .h/.m pair where I want to define the constant I do the following:

    // myfile.h
    #import <StringConsts.h>
    
    STR_CONST(MyConst, "Lorem Ipsum");
    STR_CONST(MyOtherConst, "Hello world");
    
    // myfile.m
    #define SYNTHESIZE_CONSTS
    #import "myfile.h"
    

    et voila, I have all the information about the constants in .h file only.

    0 讨论(0)
  • 2020-11-21 22:42

    You should create a header file like

    // Constants.h
    FOUNDATION_EXPORT NSString *const MyFirstConstant;
    FOUNDATION_EXPORT NSString *const MySecondConstant;
    //etc.
    

    (you can use extern instead of FOUNDATION_EXPORT if your code will not be used in mixed C/C++ environments or on other platforms)

    You can include this file in each file that uses the constants or in the pre-compiled header for the project.

    You define these constants in a .m file like

    // Constants.m
    NSString *const MyFirstConstant = @"FirstConstant";
    NSString *const MySecondConstant = @"SecondConstant";
    

    Constants.m should be added to your application/framework's target so that it is linked in to the final product.

    The advantage of using string constants instead of #define'd constants is that you can test for equality using pointer comparison (stringInstance == MyFirstConstant) which is much faster than string comparison ([stringInstance isEqualToString:MyFirstConstant]) (and easier to read, IMO).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 22:45

    If you like namespace constant, you can leverage struct, Friday Q&A 2011-08-19: Namespaced Constants and Functions

    // in the header
    extern const struct MANotifyingArrayNotificationsStruct
    {
        NSString *didAddObject;
        NSString *didChangeObject;
        NSString *didRemoveObject;
    } MANotifyingArrayNotifications;
    
    // in the implementation
    const struct MANotifyingArrayNotificationsStruct MANotifyingArrayNotifications = {
        .didAddObject = @"didAddObject",
        .didChangeObject = @"didChangeObject",
        .didRemoveObject = @"didRemoveObject"
    };
    
    0 讨论(0)
  • 2020-11-21 22:46

    Try using a class method:

    +(NSString*)theMainTitle
    {
        return @"Hello World";
    }
    

    I use it sometimes.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题