科技外文文献
1 Objective-C and Cocoa
An initial distinction should be made : Objective-C is a language, while Cocoa is a set of classes that contribute to native MacOS X programming. Theoretically, it is possible to use Objective-C without Cocoa: there is a gcc front-end. But under MacOS X, both are almost inseparable, as most of the classes supplied by the language are part of Cocoa.
More precisely, Cocoa is the implementation by Apple, for MacOS X, of the OpenStep standard,originally published in 1994. It consists of a developer framework based upon Objective-C. The GNUstep project [6] is another implementation, which is free. Its goal is to be as portable as possible on most Unix systems, and is still under development. 1.1A short history of Objective-C
It is hard to give a precise date of birth for a language, owing to the fact that there is sometime between ?rst steps, improvements, standardisation and official announcement. However, a rough history is given in Figure 1 to get a quick look at Objective-C amongst its ancestors and“challengers”.
Figure 1: Timeline of Java, C, C#, C++ and Objective-C Smalltalk-80 is one the ?rst “real” object languages. C++ and Objective-Care two different branches that build a superset of the C language. Objective-Cis very close to Smalltalk in terms of syntax and dynamism, while C++ is mu-ch more static, with the goal of having better run-time performance. Java targ-ets a C++ audience, but is also very inspired by Smalltalk for its object mod-el.That’s why, despite this document’s title, many references are made toJava.T-heC# language, developed by Microsoft, is a direct challenger to Objective-C.
Objective-C++ is a kind of merge between Objective-C and C++. It is alr-eady usable, but some behaviours are still not perfect. The goal of Objective-C++ is to mix up the syntaxes of Objective-C and C++ to bene?t from the best features of both worlds (cf. section 14 on page 64). 1.2 Objective-C 2.0
The present document has been updated to take in account the new feature-s of Objective-C 2.0,which has been released alongside MacOS X10.5. Those features are deep technical improvements,but the high-level modi?cations for the developers are easily enumerable. They can now use:
? a garbage-collector : cf. section 6.6 on page 48; ? properties : cf. section 12.2 on page 58;
? fast enumeration : cf. section 11.2.2 on page 55;
? new keywords @optional and @required for protocols : cf. section4.4 on page 22; ? updated run-time Objective-C library features : cf. section 13.2 on
page 63.
Each one is detailed in a speci?c section.
2 Syntax overview 2.1 Keywords
Objective-C is a superset of the C language. Like with C++, a well-written C program should be compile-able as Objective-C, as long as it is not using some of the bad practices allowed by C.Objective-C has only added some conceptsand their associated keywords. To avoid con?icts, these keywords begin with the @ (at) character. Here is the (short) exhaustive list: @class, @interface,@implementation, @public, @private, @protected, @try, @catch, @throw, @finally, @end,@protocol, @selector, @synchronized, @encode, @defs (no more documented in [4]). Objective-C 2.0 (cf. 1.2 on the preceding page) has added @optional, @required, @property, @dynamic,@synthesize. Let us alo quote the values nil et Nil, the types id, SEL and BOOL, the Boolean values being YES et NO. At last, a few kewords are available in particular contexts, and are notreserved outside: in, out, inout, bycopy, byref, oneway (they can be met whende?ning protocols: cf. section 4.4.5 on page 24) and getter, setter, readwrite,readonly, assign, retain, copy,nonatomic (they can be met when de?ning properties : cf. section 12.2 on page 58).
There is an easy confusion between the language keywords and some methods inherited from the root class NSObject (the mother of every class, cf. section 3.1 on page 10). For instance, the similarlooking “keywords” for memory managament, named alloc, retain, release and autorelease,are in fact methods ofNSObject. The words super and self (cf. section 3.3.1 on page 12), could alsobe understood as keywords, but self is in fact a hidden parameter to each method, and super an instruction asking the compiler to use self di?erently. However, the confusion between these false keywords and the true ones will not likely prove problematic in normal use. 2.2 Comments
The comments /* . . . */ and // are allowed. 2.3 Mixing up code and declarations
Like in C++, it is possible to insert the declaration of a variable in the middle of a block of instructions.
2.4 New types and values 2.4.1 BOOL, YES, NO
In C++, the boolean type is bool. In Objective-C, it is BOOL, which can be set to YES or NO.
2.4.2 nil, Nil and id
These three keywords are explained later in the document, but brie?y: ? Every object is of type id. This is a tool for weak-typing;
? nil is the equivalent of NULL for a pointer to an object. nil and NULL should not be interchangeable.
? Nil is the equivalent of nil for a class pointer. In Objective-C, a class is an object (it is the instance of a meta-class). 2.4.3 SEL
The SEL type can store selectors values, which are method identi?ers unrelated to any class instance
object. These values can be computed by a call to @selector. A selector can be used as a kind of
pointer to a method, even if it is not technically a real pointer to a function. See section 3.3.5 on
page 15 for more details. 2.4.4 @encode
For the purpose of interoperability, teh data types in Objective-C, even custom types, and prototypes of functions or methods, can be ASCII-encoded, according to a documented format [4]. A call to @encode(a type ) returns a C string (char*) representing the type. 2.5 Organization of source code: .h and .m ?les, inclusion
Like in C++, it is useful to split the code between interface and implementation for each class.
Objective-C uses .h ?les for headers, and .m ?les for the code; .mm ?les are used for Objective-C++
(see Section 14 on page 64). Objective-C introduces the #import directive to replace #include.
Indeed, every C header should use compilation guards to prevent multiple inclusions. This is automatic when using #import. Below is a typical interface/implementation example. The Objective-Csyntax is explained later.
2.6 Class names: why NS?
In this document, almost all class names begin with NS, like NSObject or NSString. The reason
is simple: they are Cocoa classes, and most Cocoa classes begin with NS since they were initiated
under NeXTStep.
It is a common practice to use a pre?x to identify the origin of a class. 2.7 Differencing functions and methods
Objective-C is not a language with “function calls using square brackets”.This would be a legitimate thought when observing code like that : [object doSomething];
instead of
object.doSomething(); But in fact, Objective-C is a superset of C, so that functions match the same syntax and semantics as C for declaration, implementation and call. On the contrary, methods, which do not exist in C, have a special syntax, which includes square brackets. Moreover, the difference is not only in the syntax, but also the meaning. This is detailed further in Section 3.2 on the next page: this is not a method call, this is sending a message. This is not just a simple academic distinction;it has implications on the mechanism of Objective-C. Even if it isthe same regarding the source code organization, this mechanism allows much more dynamism. For instance, it is compatible with adding a method at run-time (cf. section 13.2 on page 63). The syntax is also more readable, especiallywith nested calls (cf. section 3.3.1 on page 12).
相关推荐: