iOS & Swift

objective-c 에서 static constant 의 정의

그리다웍스 2015. 2. 6. 16:51


출처 : http://www.numbergrinder.com/2008/12/static-constant-strings-in-objective-c/


Static Constant Strings in Objective-C


One of the most commonly used features in Java is the ability to avoid string “magic constants” by putting those strings into static variables. These values are then readily accessible inside of the class, not necessarily visible outside of it, and are easy to modify on the fly if and when such is called for. They also allow me to create the string once, saving both memory and time.

When I started trying to build my iPhone application, I ran into the immediate problem: how can I abstract out my strings?

Private Static Strings

We can do the same thing in Objective-C that we can do in Java, but it is a little counter intuitive if you don’t know C.

In the class file, before the implementation, declare the constants as follows:

static NSString *const kConstant = @"myconstant";

There are several components here:

  • static – Means that the variable doesn’t have to be declared in the header. Since it is “private,” this makes sense.
  • NSString – The type of the object is NSString, though we could use some other basic type in the same way.
  • *const – This is a constant pointer. The pointer itself is constant, though in theory the actual value underlying the pointer could change at a later date. If we were to use a pointer to a constant (e.g., const NSString *) the memory contents could not change, but the pointer could be moved around. You can read more about this at cppkid.

*const에 대한 부연설명 (by cppkid)

출처 : https://cppkid.wordpress.com/2008/10/06/constant-pointer-and-pointer-to-a-constant/

Constant Pointer And Pointer To A Constant

The keyword const specifies that you can not change the value of the variable. When it comes to pointers, we can have two types of constants.

Constant Pointer

It prevents you from changing the pointer from one memory location to another. However, you can change the contents of the pointer.

1
2
3
4
5
   int myInt = 10;
   int* const pInt = &myInt;  // Constant pointer
   int myNewInt = 20;
   pInt = &myNewInt; //Illegal 
   *pInt = 30; // Legal

Pointer to a constant

It prevents you from changing the contents of the memory location. However, you can reassign the pointer to a new memory location.

1
2
3
4
5
   int myInt = 10;
   const int* pInt = &myInt;  // Pointer to a constant
   int myNewInt = 20;
   pInt = &myNewInt; //Legal 
   *pInt = 30; // Illegal



Public Static Strings

For public variables, we’ll use the extern keyword.

For this in the header file declare:

extern NSString *const kClick;

…and then in the implementation file:

NSString *const kClick = @"CLICK"

Again we see the use of the “constant pointer” discussed above, except here anyone can access the value by importing the header file.


What about #define?

Macros solve one of the problems with using string magic constants: they provide the level of abstraction so when the macro is modified, it will propagate. Unfortunately if the string is not retained and there is a release pool involved, the string will probably be disposed of and have to be recreated.

I tend to consider it a good practice to avoid preprocessor macros where possible. They have their uses, but can lead to some unpredictable–and compiler dependent–results. This is especially once we get out of the realm of using strings.

So given that there are a few technical reasons to go with one of the above methods and that #define macros have their own quirks, I consider it a “best practice” to go with either extern or static variables for string constants.