3 questions about extern used in an Objective-C project

1) you’re specifying its linkage. extern linkage allows you or any client to reference the symbol. regarding global variables: if the variable is mutable and/or needs proper construction, then you should consider methods or functions for this object. the notable exception to this is NSString constants: // MONClass.h extern NSString* const MONClassDidCompleteRenderNotification; // MONClass.m NSString* … Read more

Mixing extern and const

Yes, you can use them together. And yes, it should exactly match the declaration in the translation unit it’s actually declared in. Unless of course you are participating in the Underhanded C Programming Contest 🙂 The usual pattern is: file.h: extern const int a_global_var; file.c: #include “file.h” const int a_global_var = /* some const expression … Read more

Advantage of using extern in a header file

Introduction There are three forms of declaration of concern here:1 extern int x; // Declares x but does not define it. int x; // Tentative definition of x. int x = 0; // Defines x. A declaration makes an identifier (a name, like x) known. A definition creates an object (such as an int).2 A … Read more

Forward-declare enum in Objective-C

Most recent way (Swift 3; May 2017) to forward declare the enum (NS_ENUM/NS_OPTION) in objective-c is to use the following: // Forward declaration for XYZCharacterType in other header say XYZCharacter.h typedef NS_ENUM(NSUInteger, XYZCharacterType); // Enum declaration header: “XYZEnumType.h” #ifndef XYZCharacterType_h #define XYZCharacterType_h typedef NS_ENUM(NSUInteger, XYZEnumType) { XYZCharacterTypeNotSet, XYZCharacterTypeAgent, XYZCharacterTypeKiller, }; #endif /* XYZCharacterType_h */`

Can local and register variables be declared extern?

Local variables can be declared extern in some cases Let’s read the C99 N1256 standard draft. The standard calls “local variables” as having “block scope”. 6.7.1/5 “Storage-class specifiers” says: The declaration of an identifier for a function that has block scope shall have no explicit storage-class specifier other than extern. Then for what it means … Read more