How to declare NSString constants for passing to NSNotificationCenter

后端 未结 6 399
无人及你
无人及你 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:14

    Typically, one does not make these variables constant. One would, on the other hand, declare them as externs in the header file. This is (a little bit) because when you say const NSString *string, you are telling the compiler that the memory pointed to by string will not change -- this is both not useful and not necessarily true, being that we have no control over how apple's classes manage state variables. (Although NSStrings declared at runtime are placed in the text segment, functionality could change across versions or something.) If you truly wanted to use const, which I still advise against, it should be NSString * const string, which will prevent the pointer from being altered to point to a different memory location (which it would not do otherwise);

提交回复
热议问题