1. private
myfile.m
NSString *const SelectionSort = @”SelectionSort”;
2. public
myfile.h
extern NSString *const SelectionSort;
and in myfile.m define them
myfile.m
NSString *const SelectionSort = @”SelectionSort”;
It works, but only if you need a non global constant. Static constant is not visible outside the file. Otherwise use the 1st option.
http://stackoverflow.com/questions/6188672/where-do-you-declare-constant-in-objective-c
============================================
The #define is a pre-processor macro. That means that it basically goes through your code and replace your macro with what you’ve defined.
If you use a const, it’s going to be a pointer to the string in memory. It’s way more efficient than having the same string being allocated wherever/whenever it is used.
To do that, you’ll need both .h and .m files. Your .h file will look something like:
extern NSString * const YOUR_STRING;
And your .m file:
NSString * const YOUR_STRING = @”your string”;
no need Class.THIS_IS_A_CONST , just THIS_IS_A_CONST then ok
===============================================
把NSString宣告為常數
錯誤的宣告方式
會出現warning:(Sending ‘const NSString *__strong’ to parameter of type ‘NSString *’ discards qualifiers)
.h
extern NSString * SelectionSort;
.m
const NSString *InsertionSort = @”SelectionSort”;
正確的宣告方式
.h
extern NSString *const SelectionSort;
.m
NSString *const SelectionSort = @”SelectionSort”;
http://mywayonobjectivec.blogspot.hk/2014/05/nsstring.html