How to declare NSString constants for passing to NSNotificationCenter

后端 未结 6 405
无人及你
无人及你 2020-12-24 02:29

I\'ve got the following in my .h file:

#ifndef _BALANCE_NOTIFICATION
#define _BALANCE NOTIFICATION
const NSString *BalanceUpdateNotification
#endif
         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-24 03:10

    Apple has a recommendation in their own documentation, where among other things, the macro APPKIT_EXTERN is used instead of extern. Combined with the contents of a default header file when created in Xcode, this is a complete implementation:

    Constants.h:

    #import 
    
    #ifndef Constants_h
    #define Constants_h
    
    APPKIT_EXTERN NSString *MyConstantName;
    
    #endif
    

    Constants.m:

    #import "Constants.h"
    
    NSString *MyConstantName = @"MyConstantValue";
    

    I think the above is more or less the standard way to achieve string constants, according to Apple.

提交回复
热议问题