Assigning one NSString constant to another

I want to define constants NSStringand one of them refers to the other. For instance:

NSString * const kUrlGoogle = @"http://google.com";
NSString * const kUrlApple = @"http://apple.com";
NSString * const kUrlMicrosoft = @"http://microsoft.com";

NSString * const kUrlDefault = kUrlGoogle;

When I try to do this, I get a compile-time error in the line kUrlDefault:

The initializer element is not a compile-time constant

Is it possible to assign kUrlDefaultone of the lines above without resorting to using a macro?

Here are a few reasons why this might be useful:

  • I could check for equality without calling a method isEqual.
  • If I update a constant, kUrlDefaultit will automatically get a new value (as with all constants).
  • Saving all constants in one file in one place.
+4
3

, , const.

, , - , , const.

, , , setter. , , , .

+1

.

1) kUrlDefault + (void)initialize . . , - . .

2) kUrlDefault . . , , , .

0

, , .

, , , ++ objective-C ++ ( ++ )

( , ):

NSString *kUrlGoogle = @"http://google.com";
NSString *kUrlApple = @"http://apple.com";
NSString *kUrlMicrosoft = @"http://microsoft.com";

, , var, :

extern NSString *kUrlGoogle;
0

All Articles