How is a static constant different from an extern const?

In my iOS / Objective C projects, I often have a constants.h file with my API keys, etc. Until today, I have declared my constants as static const as follows:

 static NSString * const kAPIKey = @"wembvkejrvb43789gvbiu2bvfake"; 

This works fine, but with a bad flaw I can only create constants for NSString primitives and literals. Other objects, such as UIColor objects, cannot be stored in this constant, since they cannot be initialized with the static literal syntax (my understanding, a quote is required).

After reading some C ++ docs, I understand a few things:

  • static not required since const implicitly static.
  • The call to NSString * const x actually declares a constant and immutable value at x. I cannot change the value, but I can change what x points to.
  • This constant has an internal relationship, meaning that the value is determined immediately (presumably at compile time).

Are these conclusions correct?

How is extern const different? I assume they are connected externally (thus the extern keyword). Are they determined at runtime? Can I create a dynamic extern const that can be set with the value returned by the class method?

For example, I would like to create a global reach constant containing a UIColor value. I would like to construct this color value using the class method [UIColor colorWithRed:green:blue:alpha:] . This obviously does not work with internally related constants that I used (I assume because this happens at compile time), but is it possible to use an external constant, possibly configured in the +initialize method?

Any elaboration of the details of this behavior will be extremely helpful.

+4
source share
3 answers

Staticity is not needed because the constant is implicitly static.

No, it is not.

static when used in the scope of a file (that is, outside of any method or function) means that the variable is visible only inside this file.

extern means that the variable is defined in another file.

const means that the variable cannot be changed.

Consider the lines. Often you will have an implementation file (the name ends with .m) that defines some constant line pointer:

 NSString *const SomeString = @"some string"; 

You might want to use the same constant from other files. If so, you can add an ad to the header (the name ends with an .h) file, which tells the compiler that the variable is defined elsewhere:

 extern NSString *const SomeString; 

and this will allow you to use SomeString in any file that imports the header file. On the other hand, you may decide that you definitely do not want the constant to be used outside the implementation file. In this case, you can declare it static (in the implementation file again):

 static NSString *const SomeString = @"some string"; 

and this will prevent its use outside the file.

The call to NSString * const x actually declares a constant and immutable value at x. I cannot change the value, but I can change what x points to.

That's right, it declares the x pointer constant - you cannot change it. You also cannot change the value that it points to if it is really NSString , because the instance of NSString does not change.

This constant has an internal relationship, meaning that the value is determined immediately (presumably at compile time).

I'll take the fifth one on this - I don’t know exactly how the compiler deals with constant lines. I find it safe to use this as a mental model; the string will be defined anyway before your code ever uses it.

+8
source

In your specific programming question, how to create a compilation-defined color object: you cannot, because, in addition to the handful for which the language provides literal syntax, objects are created at runtime.

But you can still do it elegantly at runtime, without adding to the global scope, just as sdk does ...

 @interface UIColor (RainbowAddition) + (UIColor *)chartruseColor; @end @implementation UIColor (RainbowAddition) + (UIColor *)chartruseColor { // bonus: this is really chartruse... according to the internet return [self colorWithRed:0.5 green:1.0 blue:0.0 alpha:1.0]; } @end 

http://cloford.com/resources/colours/500col.htm

+1
source

Objective-C is a pure extension of C, C ++ is not.

On a global scale:

  • in C (and Objective-C), writing const equivalent to extern const (external communication);
  • in a C ++ entry, const equivalent to static const (internal reference).

Both in C (and Objective-C) and C ++ to create a const with global reach, you can define it only once in one source file, for example extern const TYPE identifier = VALUE; , and declare it (usually in the header file) like: extern const TYPE identifier; (read: I defined this constant elsewhere at the global communications level).

+1
source

Source: https://habr.com/ru/post/895460/


All Articles