P. 1
The Objective-C Programming Language - Book

The Objective-C Programming Language - Book

3.0

|Views: 689|Likes:
Published by Nikunj Soni

More info:

Published by: Nikunj Soni on Dec 01, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less

06/26/2013

Directives to the compiler begin with “@”. The following directives are used to declare and define classes,
categories, and protocols:

Definition

Directive

Begins the declaration of a class or category interface.

@interface

Begins the definition of a class or category.

@implementation

Begins the declaration of a formal protocol.

@protocol

Ends the declaration/definition of a class, category, or protocol.

@end

The following mutually exclusive directives specify the visibility of instance variables:

Definition

Directive

Limits the scope of an instance variable to the class that declares it.

@private

Limits instance variable scope to declaring and inheriting classes.

@protected

Removes restrictions on the scope of instance variables.

@public

118

Preprocessor Directives

2009-10-19 | © 2009 Apple Inc. All Rights Reserved.

APPENDIXA

Language Summary

The default is@protected.

These directives support exception handling:

Definition

Directive

Defines a block within which exceptions can be thrown.

@try

Throws an exception object.

@throw

Catches an exception thrown within the preceding@try block.

@catch()

Definesablockofcodethatisexecutedwhetherexceptionswerethrownornotinapreceding
@try block.

@finally

The following directives support the declared properties feature (see “Declared Properties” (page 67)):

Definition

Directive

Begins the declaration of a declared property.

@property

Requests that, for the properties whose names follow, the compiler generate accessor
methods for which there are no custom implementations.

@synthesize

Instructs the compiler not to generate a warning if it cannot find implementations of
accessor methods associated with the properties whose names follow.

@dynamic

In addition, there are directives for these particular purposes:

Definition

Directive

Declares the names of classes defined elsewhere.

@class

Returns the compiled selector that identifies method_name.

@selector(method_name)

Returns the protocol_name protocol (an instance of the Protocol class).
(@protocol is also valid without (protocol_name) for forward
declarations.)

@protocol(protocol_name)

Yields a character string that encodes the type structure of type_spec.

@encode(type_spec)

DefinesaconstantNSStringobjectinthecurrentmoduleandinitializes
the object with the specified string.

On Mac OS X v10.4 and earlier, the string must be 7-bit ASCII-encoded.
On Mac OS X v10.5 and later (with Xcode 3.0 and later), you can also use
UTF-16 encoded strings. (The runtime from Mac OS X v10.2 and later
supports UTF-16 encoded strings, so if you use Mac OS X v10.5 to compile
an application for Mac OS X v10.2 and later, you can use UTF-16 encoded
strings.)

@"string"

Compiler Directives

119

2009-10-19 | © 2009 Apple Inc. All Rights Reserved.

APPENDIXA

Language Summary

Definition

Directive

Defines a constantNSString object in the current module. The string
created is the result of concatenating the strings specified in the two
directives.

@"string1" @"string2" ...
@"stringN"

Defines a block of code that must be executed only by one thread at a
time.

@synchronized()

Classes

Anewclassisdeclaredwiththe@interfacedirective.Theinterfacefileforitssuperclassmustbeimported:

#import "ItsSuperclass.h"

@interface ClassName : ItsSuperclass < protocol_list >
{

instance variable declarations

}
method declarations
@end

Everything but the compiler directives and class name is optional. If the colon and superclass name are
omitted, the class is declared to be a new root class. If any protocols are listed, the header files where they’re
declared must also be imported.

A file containing a class definition imports its own interface:

#import "ClassName.h"

@implementation ClassName
method definitions
@end

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->