这是Cocoa中的双下划线 [英] What is this double underscore in Cocoa

查看:118
本文介绍了这是Cocoa中的双下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C中的单个下划线显然是为苹果的内部使用保留的(并且可以在Apple的声明之前使用私有的实例变量)。但是为什么他们会在他们的SQLiteBooks示例中为iPhone使用一个 double -underscore?查看从MasterViewController.m取得的片段:

  +(EditingViewController *)editingViewController {
//实例化编辑视图控制器。
if(__editingViewController == nil){
__editingViewController = [[EditingViewController alloc] initWithNibName:@EditingViewbundle:nil];
}
return __editingViewController;
}

这里提到了双下划线的使用论坛,因为它涉及到C - 它是compier的内部使用。 。我想我不明白这是如何适用于这种情况。



我需要一个ViewController在我的应用程序,将表现得像在SQLiteBooks示例项目中的一个,但这个双下划线让我困惑。

解决方案

C编译器和Objective-C编译器都不会将带有前导下划线的变量名称与任何其他变量名称区别对待。单引号或双引号下划线只是一个常规,并且有效地形成一个命名空间,就像 NS 前缀用于Cocoa类如 NSString



查看SQLiteBooks代码, MasterViewController.m 静态全局变量:

  //从这个类管理编辑视图控制器,以便可以从细节和添加控制器。 
static EditingViewController * __ editingViewController = nil;

所以我的猜测是SQLiteBooks的作者使用双前缀下划线来表示一个全局变量。 / p>

C编译器(通过扩展Objective-C)保留以两个下划线和大写字母开头的名称,供编译器供应商使用,为它们提供保留的命名空间,用于全局用于实现标准库的变量和函数,或引入新的非标准关键字,例如 __ block



虽然SQLiteBooks代码在技术上是有效的,但在我看来,它太容易与保留的命名空间混淆。如果你重用这个代码,我建议重命名那个变量(Xcode有一个非常好的重命名重构,它会自动为你做)。


The single underscore in Objective-C is apparently reserved for Apple's "internal" use (and was available for use with private instance variables prior to Apple's claim). But why would they use a double-underscore in their SQLiteBooks example for the iPhone? 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;
}

There's mention of double-underscore use on this forum as it relates to C - it's for the "compier's internal use." I guess I don't see how that's applicable in this situation.

I need a ViewController in my app that would behave much like the one in the SQLiteBooks example project but this double-underscore has me perplexed.

解决方案

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

Looking at 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;

So my guess is that the author of SQLiteBooks uses a double leading underscore to indicate a global variable.

C compilers (and by extension Objective-C) reserve names beginning with two underscores and a capital letter for use by the compiler vendor, giving them a reserved namespace to use for global variables and functions used to implement standard libraries, or to introduce new non-standard keywords like __block.

While the SQLiteBooks code is technically valid, it's too easily confused with the reserved namespace in my opinion. If you do reuse that code, I'd recommend renaming that variable (Xcode has a very nice rename refactoring that will do it automatically for you).

这篇关于这是Cocoa中的双下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆