Objective-C中保留的关键字? [英] Reserved keywords in Objective-C?

查看:284
本文介绍了Objective-C中保留的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天在CocoaHeadsÖresund会议上, peylow 构建了一个伟大的ObjC测验。比赛非常激烈,当最后一个问题被评估时,有三个人得分相同:Objective-C添加到C中有多少个保留的关键字?



接下来是激烈的辩论。所有同意 @interface @implementation 等都是预处理器指令,而不是关键字, ?它可能是一个关键字,但不是一个保留的关键字。例如,下面将编译没有错误或警告:

  NSArray * in; 
for(in in)
NSLog(@bwahahaa);

我们得出结论,ObjC向C添加了个保留关键字,一个看起来很好的书。



但今天我尝试一些更系统的滥用编译器,尝试这样:

  int self = 45; 
self ++;
int y = self;

编译良好,同样的代码也可以代替 BOOL,bycopy ,inout,oneway,byref,SEL, IMP



使用 id 作为变量名,第一行和最后一行编译,但不是第二行。 协议也是如此。



使用 super ,第一行编译,但不是第二和第三行。



YES NO NULL ,所有三行都无法编译,只是定义为 true false nil



在我看来,很多这是gcc变得困惑,我不太确定它反映了什么是和不是一个保留的关键字在Objective-C 。例如,为什么可以使用 self 作为int的名称,但不是 super

第一个赋值总是起作用(除了YES,NO和NULL),这似乎支持这样的想法,即没有一个候选者是技术上保留的关键字发现在C.或?



有人可以给我们这个棘手问题的权威解释吗?



人民的荣誉是危险的。



编辑:正如尼古拉Ruhe指出,我们需要一个清晰的关键字定义继续。尼科引用了一篇维基百科文章说,关键字是一个字或具有特定意义的标识符。



我认为从同一篇文章中使用这个定义是合理的:


在许多语言中,例如C和
类似的环境,如C ++,a
关键字是​​一个保留字其中
标识语法形式。词
用于控制流构造,如
as if,then和else是关键字。在
这些语言中,关键字不能也用
作为变量名或
函数。


此外,如文章所述:


通常,当程序员尝试
使用变量或
函数名,将触发编译错误


在这个意义上, 在语言的正式规范中预定义,且不能用作用户定义的名称的关键字

解决方案

所有同意@interface,@implementation等都是预处理器指令,



然后都错了。 #import #pragma 是预处理器指令。
@interface,@implementation,@protocol 等是Objective-C的关键字,它们是编译器指令。他们没有是预处理器指令,因为NeXT扩展GCC编译Objective-C没有Stepstone的原始Objective-C预处理器实现。


At the CocoaHeads Öresund meeting yesterday, peylow had constructed a great ObjC quiz. The competition was intense and three people were left with the same score when the final question was to be evaluated: How many reserved keywords does Objective-C add to C?

Some spirited debate followed. All agreed that @interface, @implementation etc are all pre-processor directives rather than keywords, but how about something like in? It might be a keyword, but it's not a reserved keyword. For example, the following will compile without errors or warnings:

NSArray* in;
for (in in in)
   NSLog(@"bwahahaa");

We concluded that ObjC adds no reserved keywords to C, and someone won a seemingly well-earned book.

But today I tried some more systematic abuse on the compiler by trying things like this:

int self = 45;
self++;
int y = self;

That compiles fine, and the same code works replacing self for BOOL, bycopy, inout, oneway, byref, SEL, and IMP.

Using id as the variable name, the first and last lines compile, but not the second one. The same goes for Protocol, and Class.

Using super, the first line compiles, but not the second and third.

With YES, NO, and NULL, all three lines fail to compile, probably because they are just defined as true, false, and nil.

It looks to me like a lot of this is gcc getting confused and I'm not so sure it reflects what is and isn't a reserved keyword in Objective-C. Why, for example, is it ok to use self as the name of an int, but not super?

The fact that the first assignment always works (except for with YES, NO, and NULL) would seem to support the idea that none of the candidates are technically reserved keywords that are not found in C. Or?

Could someone please give us an authoritative explication of this thorny issue?

Several people's honor is at stake.

EDIT: As Nikolai Ruhe pointed out, we need a clear definition of "keyword" to proceed. Niko cited a Wikipedia article saying that a keyword is "a word or identifier that has a particular meaning".

I think it is reasonable to use this definition from the same article:

In many languages, such as C and similar environments like C++, a keyword is a reserved word which identifies a syntactic form. Words used in control flow constructs, such as if, then, and else are keywords. In these languages, keywords cannot also be used as the names of variables or functions.

Furthermore, as the article states:

Typically, when a programmer attempts to use a keyword for a variable or function name, a compilation error will be triggered.

In this sense, then, are there any reserved keywords that are predefined in the language’s formal specifications and cannot be used as a user-defined name?

解决方案

All agreed that @interface, @implementation etc are all pre-processor directives rather than keywords

Then all were mistaken. #import and #pragma are preprocessor directives. @interface, @implementation, @protocol and so forth are keywords of Objective-C, and they are compiler directives. They haven't been preprocessor directives since NeXT extended GCC to compile Objective-C without a Stepstone's original Objective-C pre-processor implementation.

这篇关于Objective-C中保留的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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