What is the difference between the following definition of const

Usually I use the first to determine const, but I do not know the difference in the following clearly.

static NSString* kFetcherCallbackThreadKey = @"_callbackThread"; static NSString* const kFetcherCallbackRunLoopModesKey = @"_runLoopModes"; NSString* const kFetcherRetryInvocationKey = @"_retryInvocation"; static const NSUInteger kMaxNumberOfNextLinksFollowed = 25; 
+7
source share
3 answers

In C, the static used outside a function is used to declare a character that will only be accessible from the file in which it was declared. Type of "private" global variables.

The keyword const means "constant." Read, the value cannot be changed. Note that the two statements are different:

 const int * x; int * const x; 

The first defines a pointer to a constant integer (its value cannot be changed, but it can point to something else). The second defines a constant pointer to an integer (the value of the pointer cannot be changed, but the value of int can be). So you can perfectly:

 const int * const x; 

So in your case:

 static NSString* kFetcherCallbackThreadKey = @"_callbackThread"; 

Pointer to an instance of NSString that will be available only from the file in which it was declared.

 static NSString* const kFetcherCallbackRunLoopModesKey = @"_runLoopModes"; 

A constant pointer to an instance of NSString, which will be available only from the file in which it was declared.

 NSString* const kFetcherRetryInvocationKey = @"_retryInvocation"; 

A constant pointer to an instance of NSString, which can be accessed from other files in your project.

 static const NSUInteger kMaxNumberOfNextLinksFollowed = 25; 

A constant that will be available only from the file in which it was declared.

+2
source

static means that the variable is only available within the compilation unit in which it was specified, essentially this source file. const means that its value can never change. You can use one or both depending on what you are looking for.

+2
source

This is a static string that will be the same reference for all instances of the class (static). If you change it in one instance, it will change in all other cases.

 static NSString* kFetcherCallbackThreadKey = @"_callbackThread"; 

This is an NSString pointer to a persistent object that is also shared among all instances (static). The const directive makes a variable immutable.

 static NSString* const kFetcherCallbackRunLoopModesKey = @"_runLoopModes"; 

This is a pointer to an NSString persistent object. It can have a different instance for each class (if NSStrings are not interned by the compiler, I'm not sure if they exist), but cannot be changed (const).

 NSString* const kFetcherRetryInvocationKey = @"_retryInvocation"; 

This is a constant static integer. It will be shared between all instances of the class (static) and cannot be changed (const).

 static const NSUInteger kMaxNumberOfNextLinksFollowed = 25; 
0
source

All Articles