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

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

问题描述

Objective-C 中的单个下划线显然是为 Apple 的内部"使用保留的(并且在 Apple 声明之前可用于私有实例变量).但是为什么他们要在 iPhone 的 SQLiteBooks 示例中使用 双下划线?请参阅摘自 MasterViewController.m 的此片段:

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;
}

在此中提到了双下划线的使用论坛,因为它与 C 相关 - 供编译器内部使用".我想我不明白这在这种情况下如何适用.

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.

我的应用程序中需要一个 ViewController,它的行为与 SQLiteBooks 示例项目中的非常相似,但这个双下划线让我感到困惑.

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.

推荐答案

无论是 C 编译器还是 Objective-C 编译器都不会以与任何其他变量名不同的方式处理带有前导下划线的变量名.单个或双前导下划线只是一个约定,并有效地形成了一个命名空间,很像 NSString 等 Cocoa 类中使用的 NS 前缀.

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.

查看SQLiteBooks代码,MasterViewController.m定义了这个静态全局变量:

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;

所以我的猜测是 SQLiteBooks 的作者使用双前导下划线来表示全局变量.

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

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

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.

虽然 SQLiteBooks 代码在技术上是有效的,但在我看来,它很容易与保留的命名空间混淆.如果您确实要重用该代码,我建议您重命名该变量(Xcode 有一个非常好的重命名重构功能,它会自动为您完成).

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).

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

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