SoFunction
Updated on 2025-04-11

iOS keyword static extern const usage example detailed explanation

Global variables

Variables declared outside the function can be attached with initial values ​​when declared, stored in the global area, and the life cycle is the entire program run.

#import ""
//Define this class when it is introduced by other files when it is defined in .h file Times of repeated definition error (1 duplicate symbol for architecture x86_64)NSString * SEString = @"SEString";
@implementation SEObject
@end
#import ""
//#import ""
//NSString * SEString;
@implementation SEView
@end

The same object name cannot exist in the source program, otherwise the compiler will report an error (1 duplicate symbol for architecture x86_64

extern

At this time, if other source files want to access the global variable, they need to declareextern

  • In the class usedextern global variable, At this time, do not introduce the class where the global variable is located.
#import ""
//#import ""
//NSString * SEString;
@implementation SEView
- (void)add {
    extern NSString * SEString;
    NSLog(@"%@",SEString);
    SEString = @"SEString2";
    NSLog(@"%@",SEString);
}
@end
  • In the class where the global variable is located.h fileDeclare the external use of the global variable (recommend)。
#import <Foundation/>
NS_ASSUME_NONNULL_BEGIN
extern NSString * SEString;
@interface SEObject : NSObject
@end
NS_ASSUME_NONNULL_END
#import ""
#import ""
//NSString * SEString;
@implementation SEView
- (void)add {
    //extern NSString * SEString;
    NSLog(@"%@",SEString);
    SEString = @"SEString2";
    NSLog(@"%@",SEString);
}
@end
SEString
SEString2

static - Static global variables

The global variables modified with static are static global variables, stored in the global area, and the life cycle is during the entire program run.

#import ""
//Define this class when it is introduced by other files when it is defined in .h file Times of repeated definition error (1 duplicate symbol for architecture x86_64)static NSString * SEString = @"SEString";
@implementation SEObject
+ (void)add {
    NSLog(@"%@",SEString);
    SEString = @"SEString2";
    NSLog(@"%@",SEString);
}
SEString
SEString2
@end

staticCan't be withexternUse in combination, otherwise an error will be reported:Cannot combine with previous 'extern' declaration specifier

When declaring that this class is introduced in the .h file, you can still use and modify this static global variable;

When declaring that in the .m file, the two class files use the same variable name and are independent of each other.

The difference between global variables and static variables (excerpt)

Although the difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. Static global variables limit their scope, that is, they are only valid in the source file that defines the variable, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file and can only be common to functions within that source file, errors can be avoided in other source files.

const

constModified variables are immutable.

Correct usage:

static NSString * const SEString = @"SEString";

The following two ways of writingconstThe modified one is* SEString,*It is a pointer pointer symbol, which means that at this timePointing to memory addresses is immutable,andMemory saved content is variable

static NSString const * SEString = @"SEString";
static const NSString * SEString = @"SEString";

Local variables

Variables declared internally by the function only exist during the current function execution.

@implementation SEObject

- (void)add {
    NSInteger a = 1;
    NSInteger b = 2;
    NSInteger c = a+b;
    NSLog(@"c = %ld",c);
}

@end

static - Static local variables

Local variables modified with static are static local variables, stored in the global area, and the life cycle is the entire program run.

- (void)add {
    NSInteger a = 1;
    NSInteger b = 2;
    static NSInteger c2;
    c2 += a+b;
    NSLog(@"c2 = %ld",c2);
}
Called twice:
c2 = 3
c2 = 6

The above is the detailed explanation of the iOS keyword static extern const usage example. For more information about iOS keyword static extern const, please follow my other related articles!