What is this double underscore in Cocoa

The only underscore in Objective-C seems to be reserved for use internally by Apple (and was available for use with private instance variables prior to Apple's approval). But why do they use double underscores in their SQLiteBooks for iPhone example? See this snippet taken from MasterViewController.m:

+ (EditingViewController *)editingViewController { // Instantiate the editing view controller if necessary. if (__editingViewController == nil) { __editingViewController = [[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil]; } return __editingViewController; } 

The use of double underscores in this forum is mentioned here, as it refers to C - it for "internal use of the compiler." I probably don’t see how it is applicable in this situation.

I need a ViewController in my application that will behave the same as in the SQLiteBooks sample project, but this double underscore puzzled me.

+6
objective-c cocoa double-underscore
source share
4 answers

Neither the C compiler nor the Objective-C compiler handle variable names with leading underscores differently than any other variable name. A single or double underscore is just a convention and effectively forms a namespace, like the NS prefix used in Cocoa classes such as NSString .

Considering the SQLiteBooks code, MasterViewController.m defines this static global variable:

 // Manage the editing view controller from this class so it can be easily accessed from both the detail and add controllers. static EditingViewController *__editingViewController = nil; 

Therefore, I assume that the author of SQLiteBooks uses a double-digit underscore to indicate a global variable.

C compilers (and with the extension Objective-C) fallback names starting with two underscores and capital letters for use by the compiler provider, providing them with a reserved namespace for use with global variables and functions used to implement standard libraries, or to introduce new non-standard keywords like __block .

Although SQLiteBooks code is technically sound, it mixes too easily with the reserved namespace, in my opinion. If you reuse this code, I would recommend renaming this variable (Xcode has a very good renaming refactoring that will do this automatically for you).

+20
source share

In the compiler, underscores are treated like any alphabetic character. In general, underscores are commonly used by language extensions or large libraries to avoid conflicts with user code.

Using underscores is problematic because many groups try to reserve each name with a specific combination of underscores.

Apple has traditionally used a single underscore prefix to denote a private instance variable (common style in object-oriented languages). This was done to suggest that everyone should prefix their ivars with underscores until Apple indicated that using the underscore in your code could lead to conflicts with Cocoa if Apple decides to change its headers, and maybe you shouldn't. Thus, underscore prefixes have become the "not recommended" coding practice.

In C and C derivative languages, any double underlined word before and next is a non-standard language extension. See Apple Extensions such as __attribute __

Trailing underscores are often added as the name of the compiler or debugger for modified versions of the original names (especially when the compiler is multi-pass), and they are usually avoided to remain distinctly different from the originals. Google suffix their Objective-C local instance variables with underscores to avoid conflicts with Apple underscores.

My advice: do not use underscores. You should not use local variables with the same name as instance variables (which is just confusing). The only potential conflict between the parameters in the setter methods and the corresponding instance variables is that you probably need a prefix parameter with the lower case "a", "new" (or similar), as this clearly indicates that the parameter is input values ​​but not yet " value".

+6
source share

This is just a variable naming convention. He does not do anything. This is a way for programmers to remind themselves: "This is a private variable."

+2
source share

It is not uncommon for compiler / library providers to designate certain pre / postfix as β€œreserved” for them. This largely eliminates any unintended conflicts between types / defines / inherited variables.

The message you are referring to is about definitions, not variables. Many compilers use double underscores for the definitions they provide and rely on.

Regarding why the sample code uses this style, the original author used the same coding style that he probably uses in everyday work, and a potential conflict never stood out.

You should keep the code sample well as is, but if this is not convenient for you, you can rename the variable.

0
source share

All Articles