Documente Academic
Documente Profesional
Documente Cultură
m
.c
.mm
@interface
@interface
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
...
}
id
id
isa
isa
isa
id
id anObject;
id
int
nil
id
id
id
objc/objc.h
0 id nil
int
isa
isa
isa
[receiver message]
myRectangle
display
[myRectangle display];
[myRectangle setWidth:20.0];
myRectangle
setOrigin::
setOriginX:y:
[myRectangle setOriginX: 30.0 y: 50.0]; // This is a good example of multiple
arguments
makeGroup:
isFilled
YES
myRectangle
NO
BOOL isFilled;
isFilled = [myRectangle isFilled];
nil
nil
nil
0 nil
aPerson
nil
mother
nil
nil
float
double
long double
long long
nil
sizeof(void*)
0
nil
0.0
nil
nil
id anObjectMaybeNil = nil;
// this is valid
if ([anObjectMaybeNil methodThatReturnsADouble] == 0.0)
{
// implementation continues...
}
nil
void
nil
sizeof(void*)
nil
nil
nil
sizeof(void*)
primaryColor
otherRect
primaryColor
isFilled
display
display
id
copy
copy
display
.
[]
myInstance.value = 10;
printf("myInstance value: %d", myInstance.value);
[myInstance setValue:10];
printf("myInstance value: %d", [myInstance value]);
@"Hello"
NSString
set
set
NSMutableData
id y;
x = y.z;
// z is an undeclared property
z
z
z
z
BOOL
readonly
nil
nil
// each member of the path is an object
x = person.address.street.name;
x = [[[person address] street] name];
// the path contains a C struct
// will crash if window is nil or -contentView returns nil
y = window.contentView.bounds.origin.y;
y = [[window contentView] bounds].origin.y;
// an example of using a setter....
person.address.street.name = @"Oxford Road";
[[[person address] street] setName: @"Oxford Road"];
self
self
self.age = 10;
self.
age
age = 10;
aVariable = anObject.aProperty;
aProperty
aProperty
aVariable
aVariable
setName
anObject
setName
@"New Name"
name
void
xOrigin = aView.bounds.origin.x;
bounds
NSRect
xOrigin
bounds
origin.x
setName:
NSInteger i = 10;
anObject.integerProperty = anotherObject.floatProperty = ++i;
11
anObject.integerProperty
anotherObject.floatProperty
setIntegerProperty:
setFloatProperty:
anObject.retain;
setFooIfYouCan:
(void)
flag = aView.lockFocusIfCanDraw;
lockFocusIfCanDraw
flag
flag
/* property declaration */
@property(readonly) NSInteger readonlyProperty;
/* method declaration */
- (void) setReadonlyProperty: (NSInteger)newValue;
/* code fragment */
self.readonlyProperty = 5;
readonly
to readonly property 'readonlyProperty'
warning: assignment
readwrite
NSMatrix
NSText
NSArray
NSWindow
NSWindow
NSDictionary
NSFont
NSObject
NSObject
Graphic
Image
Text
Shape
Line
Rectangle
Circle
Square
NSObject
NSObject
NSObject
NSObject
NSObject
NSObject
NSObject
NSObject
NSObject
NSObject
NSObject
NSObject
isa
isa
Class
NSPoint
NSColor
Pattern
...
float
float
BOOL
NSColor
...
isa;
origin;
*primaryColor;
linePattern;
width;
height;
filled;
*fillColor;
declared in NSObject
declared in Graphic
declared in Shape
declared in Rectangle
NSObject
NSObject
display
display
display
NSObject
NSObject
NSView
sizeof
int i = sizeof(Rectangle);
id
Rectangle *myRectangle;
id
id
id
Graphic *myRectangle;
myRectangle
isMemberOfClass:
NSObject
if ( [anObject isMemberOfClass:someClass] )
...
isKindOfClass:
NSObject
if ( [anObject isKindOfClass:someClass] )
...
isKindOfClass:
YES
NSObject
isMemberOfClass:
isKindOfClass:
NSObject
int versionNumber = [Rectangle version];
id
id aClass = [anObject class];
class
id
myRectangle
id myRectangle;
myRectangle = [Rectangle alloc];
alloc
0
isa
init
myRectangle
alloc
init
alloc
init
initWithPosition:size:
NSMatrix
NSCell
NSMatrix
NSMatrix
NSCell
NSCell
NSObject
NSCell
NSBrowserCell
NSActionCell
NSButtonCell
NSTextFieldCell
NSSliderCell
NSFormCell
NSMenuCell
NSCell
NSTextFieldCell
NSCell
NSMatrix
NSButtonCell
NSMatrix
NSMatrix
NSMatrix
NSCell
NSCell
NSMatrix
NSMatrix
NSMatrix
NSMatrix
NSCell
NSCell
NSMatrix
[myMatrix setCellClass:[NSButtonCell class]];
NSMatrix
NSMatrix
setCellClass:
int MCLSGlobalVariable;
@implementation MyClass
// implementation continues
static
static
static
initialize
initialize
initialize
initialize
initialize
initialize
initialize
initialize
initialize
initialize
initialize
initialize
initialize
initialize
+ (void)initialize
{
if (self == [ThisClass class]) {
// Perform initialization here.
...
}
}
initialize
initialize
initialize
NSObject
NSObject
NSObject
Rectangle *anObject;
anObject
id
class
isKindOfClass:
if ( [anObject isKindOfClass:[Rectangle class]] )
...
NSClassFromString
NSString *className;
...
if ( [anObject isKindOfClass:NSClassFromString(className)] )
...
nil
class
class
.m
.h
Rectangle.h
Rectangle.m
@interface
@end
@interface ClassName : ItsSuperclass
{
instance variable declarations
}
method declarations
@end
NSObject
float width;
float height;
BOOL filled;
NSColor *fillColor;
+ alloc;
- (void)display;
radius
radius
- (float)radius;
- (void)setRadius:(float)aRadius;
id
alloc
- (void)setWidth:(float)width height:(float)height;
- makeGroup:group, ...;
id
#import
#import "Rectangle.h"
#include
#include
#import "ItsSuperclass.h"
@interface ClassName : ItsSuperclass
{
instance variable declarations
}
method declarations
@end
NSObject
@class
@class Rectangle, Circle;
- (void)setPrimaryColor:(NSColor *)aColor;
NSColor
@class
@class
@class
@implementation
@end
@implementation ClassName : ItsSuperclass
{
instance variable declarations
}
method definitions
@end
Rectangle.m
Rectangle.h
#import "ClassName.h"
@implementation ClassName
method definitions
@end
+ (id)alloc
{
...
}
- (BOOL)isfilled
{
...
}
- (void)setFilled:(BOOL)flag
{
...
}
#import <stdarg.h>
...
- getGroup:group, ...
{
va_list ap;
va_start(ap, group);
...
}
. ->
filled
- (void)setFilled:(BOOL)flag
{
filled = flag;
...
}
filled
->
twin
@interface Sibling : NSObject
{
Sibling *twin;
int gender;
struct features *appearance;
}
twin
- makeIdenticalTwin
{
if ( !twin ) {
twin = [[Sibling alloc] init];
twin->gender = gender;
twin->appearance = appearance;
}
return twin;
}
- (BOOL)isFilled
{
return filled;
}
@private
@protected
@public
@package
@package
@private
@public
private_extern
@private
@protected
@public
@private
@protected
A class that
inherits the
instance variable
@public
Unrelated code
age
boss
@interface Worker : NSObject
{
char *name;
@private
int age;
char *evaluation;
@protected
id job;
float wage;
evaluation
name job
wage
@public
id boss;
}
name
@protected
job
- promoteTo:newPosition
{
id old = job;
job = newPosition;
return old;
}
promoteTo:
job
@private
@private
@public
@public
self
super
reposition
setOrigin::
setOrigin::
reposition
self
super
reposition
- reposition
{
...
[self setOrigin:someX :someY];
...
}
- reposition
{
...
[super setOrigin:someX :someY];
...
}
self
super
reposition
self
super
self
super
super
super
objc_msgSend
super
self
self
super
negotiate
makeLastingPeace
negotiate
superclass
High
negotiate
superclass
Mid
negotiate
makeLastingPeace
superclass
Low
negotiate
makeLastingPeace
makeLastingPeace
self
negotiate
- makeLastingPeace
{
[self negotiate];
...
}
negotiate
super
self
- makeLastingPeace
{
[super negotiate];
...
}
negotiate
makeLastingPeace
negotiate
super
makeLastingPeace
negotiate
negotiate
negotiate
super
negotiate
makeLastingPeace
negotiate
negotiate
super
- negotiate
{
...
return [super negotiate];
}
super
init
init
init
init
- (id)init
{
if (self = [super init]) {
...
}
}
super
super
isa
alloc
allocWithZone:
NSObject
super
super
self
self
self
super
self
self
+ (Rectangle *)rectangleOfColor:(NSColor *) color
{
self = [[Rectangle alloc] init]; // BAD
[self setColor:color];
return [self autorelease];
}
self
+ (id)rectangleOfColor:(NSColor *)color
{
id newInstance = [[Rectangle alloc] init]; // GOOD
[newInstance setColor:color];
return [newInstance autorelease];
}
alloc
self
alloc
rectangleOfColor:
array
NSMutableArray
+ (id)rectangleOfColor:(NSColor *)color
{
id newInstance = [[self alloc] init]; // EXCELLENT
[newInstance setColor:color];
return [newInstance autorelease];
}
NSArray
NSObject
alloc
alloc
allocWithZone:
NSObject
allocWithZone:
isa
0
init
NSView
initWithFrame:
init...
isa
init...
init
NSObject init
NSObject
isa
self NSObject
initWithName:
initWithName:
init...
initFromFile:
init...
nil
init...
nil
alloc
allocWithZone:
init
init...
nil
isa
0
init
initWithFormat: initWithObjects:
initWithObjectsAndKeys:
id
id
NSString
NSMutableString
NSMutableString
NSString
initWithFormat:
NSString
NSObject
init
self
self
nil
NSObject
creationDate
- (id)init {
// Assign self to value returned by super's designated initializer
// Designated initializer for NSObject is init
if (self = [super init]) {
creationDate = [[NSDate alloc] init];
}
return self;
}
initWithName:fromURL:
setEnabled: setFriend:
setDimensions:
NSView
- (id)initWithImage:(NSImage *)anImage {
// Find the size for the new instance from the image
NSSize size = anImage.size;
NSRect frame = NSMakeRect(0.0, 0.0, size.width, size.height);
// Assign self to value returned by super's designated initializer
// Designated initializer for NSView is initWithFrame:
if (self = [super initWithFrame:frame]) {
image = [anImage retain];
}
return self;
}
[self release]
nil
nil
dealloc
[self release]
dealloc
nil
release
nil
- (id)init {
if (self = [super init]) {
creationDate = [[NSDate alloc] init];
}
return self;
}
- (id)initWithImage:(NSImage *)anImage {
if (anImage == nil) {
[self release];
return nil;
}
// Find the size for the new instance from the image
NSSize size = anImage.size;
NSRect frame = NSMakeRect(0.0, 0.0, size.width, size.height);
// Assign self to value returned by super's designated initializer
// Designated initializer for NSView is initWithFrame:
if (self = [super initWithFrame:frame]) {
image = [anImage retain];
}
return self;
}
NSError
- (id)initWithURL:(NSURL *)aURL error:(NSError **)errorPtr {
if (self = [super init]) {
NSData *data = [[NSData alloc] initWithContentsOfURL:aURL
options:NSUncachedRead error:errorPtr];
if (data == nil) {
// In this case the error object is created in the NSData initializer
[self release];
return nil;
}
// implementation continues...
init...
super
- (id)initWithName:(NSString *)string {
if ( self = [super init] ) {
name = [string copy];
}
return self;
}
super
NSObject
initWithName:
init
init
Class A
Class B
initWithName:
init
initWithName:
init
init
- init {
return [self initWithName:"default"];
}
initWithName:
init
initWithName:
init
Class A
init
Class B
initWithName:
initWithName:
super
initWithName:fromFile:
init
initWithName:
initWithName:
initWithName:fromFile:
- initWithName:(char *)string {
return [self initWithName:string fromFile:NULL];
}
init
initWithName:fromFile:
initWithName:
init
Class B
initWithName:
Class C
initWithName:
initWithName:fromFile:
init
initWithName:fromFile:
super
initWithName:
init
init
initWithName:
initWithName:fromFile:
init
initWithName:
initWithName:fromFile:
initWithName:
super
super
self
self
super
init
Class A
init
Class B
initWithName:
Class C
initWithName:
initWithName:fromFile:
init
self
initWithName:
initWithName:
NSArray
+ (id)array;
NSString
+ (id)arrayWithObject:(id)anObject;
+ (id)arrayWithObjects:(id)firstObj, ...;
id
listFromFile:
init...
initWithName:
init...
soloist
+ (Soloist *)soloist {
static Soloist *instance = nil;
if ( instance == nil ) {
instance = [[self alloc] init];
}
return instance;
}
Soloist *
- (void)mouseDown:(NSEvent *)theEvent;
- (void)mouseDragged:(NSEvent *)theEvent;
- (void)mouseUp:(NSEvent *)theEvent;
helpOut:
assistant
- setAssistant:anObject
{
assistant = anObject;
}
assistant
- (BOOL)doWork
{
...
if ( [assistant respondsToSelector:@selector(helpOut:)] ) {
[assistant helpOut:self];
return YES;
}
return NO;
}
assistant
helpOut:
- (NSXMLElement *)XMLRepresentation;
- initFromXMLRepresentation:(NSXMLElement *)xmlString;
NSMatrix
NSCell
NSCell
NSMatrix
NSMatrix
NSMatrix
@protocol
@protocol ProtocolName
method declarations
@end
@protocol MyXMLSupport
- initFromXMLRepresentation:(NSXMLElement *)XMLElement;
- (NSXMLElement *)XMLRepresentation;
@end
@optional
@required
@optional
@required
@required
@optional
@protocol MyProtocol
- (void)requiredMethod;
@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
@required
- (void)anotherRequiredMethod;
@end
NSObject
NSObject
@protocol()
@protocol()
@protocol()
conformsToProtocol:
if ( ! [receiver conformsToProtocol:@protocol(MyXMLSupport)] ) {
// Object does not conform to MyXMLSupport protocol
// If you are expecting receiver to implement methods declared in the
// MyXMLSupport protocol, this is probably an error
}
conformsToProtocol:
conformsToProtocol:
respondsToSelector:
conformsToProtocol:
conformsToProtocol:
- (id <Formatting>)formattingService;
id <MyXMLSupport> anObject;
respondsToSelector:
isKindOfClass:
Formatter *anObject;
id <Formatting> anObject;
conformsToProtocol:
id <Paging> someObject;
conformsToProtocol:
if ( [anotherObject conformsToProtocol:@protocol(Paging)] )
...
NSObject
@interface Pager : NSObject < Paging >
#import "B.h"
@protocol A
- foo:(id <B>)anObject;
@end
#import "A.h"
@protocol B
- bar:(id <A>)anObject;
@end
@protocol
@protocol B;
@protocol A
- foo:(id <B>)anObject;
@end
@protocol
@interface
@property @property
@property
- (float)value;
- (void)setValue:(float)newValue;
@property(attribute [, attribute2,
...])
@synthesize
copy
set
setFoo:
foo
readonly
getter=getterName
setter=
setter=setterName
void
readonly
setter=
getter
is
readwrite
@implementation
@synthesize
readonly
readonly
@synthesize
@implementation
assign
NSInteger
retain
CGRect
assign
retain
retain
assign
release
retain
copy
assign
release
copy
NSCopying
assign
retain
copy
assign retain
copy
NSCopying
nonatomic
nonatomic
nonatomic
__attribute__
@property CGFloat x
AVAILABLE_MAC_OS_X_VERSION_10_1_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_4;
@property CGFloat y __attribute__((...));
IBOutlet
@property (nonatomic, retain) IBOutlet NSButton *myButton;
IBOutlet
__weak
__strong
@synthesize
@dynamic
@implementation
@property
@synthesize
@dynamic
readonly
@synthesize
@synthesize
@implementation
property=ivar
@synthesize firstName, lastName, age = yearsOld;
age
firstName lastName
age
yearsOld
@synthesize
@interface
@dynamic
@dynamic
readonly
readonly
readwrite
readwrite
@synthesize
NSString
readonly
NSArray
NSDictionary
readwrite
copy
copy
NSMutableString
-(void)setString:(NSString *)newString {
if (string != newString) {
[string release];
string = [newString copy];
}
}
copy
- (void)setMyArray:(NSMutableArray *)newArray {
if (myArray != newArray) {
[myArray release];
myArray = [newArray mutableCopy];
}
}
@end
dealloc
dealloc
assign
assign
dealloc
nil
- (void)dealloc {
[property release];
[super dealloc];
}
- (void)dealloc {
[self setProperty:nil];
[super dealloc];
}
retain
__strong
...
__strong CGImageRef myImage;
...
@property CGImageRef myImage;
next
next
creationTimestamp
name
gratuitousFloat
nameAndAge
next
dynamic
dynamic
nameAndAgeAsString
@protocol Link
@property id <Link> next;
@end
@implementation MyClass
@synthesize creationTimestamp = intervalSinceReferenceDate, name;
// Synthesizing 'name' is an error in legacy runtimes;
// in modern runtimes, the instance variable is synthesized.
@synthesize next = nextLink;
// Uses instance variable "nextLink" for storage.
@dynamic gratuitousFloat;
// This directive is not strictly necessary.
- (CGFloat)gratuitousFloat {
return gratuitousFloat;
}
- (void)setGratuitousFloat:(CGFloat)aValue {
gratuitousFloat = aValue;
}
- (NSString *)nameAndAgeAsString {
return [NSString stringWithFormat:@"%@ (%fs)", [self name],
[NSDate timeIntervalSinceReferenceDate] intervalSinceReferenceDate];
}
- (id)init {
if (self = [super init]) {
intervalSinceReferenceDate = [NSDate timeIntervalSinceReferenceDate];
}
return self;
}
- (void)dealloc {
[nextLink release];
[name release];
[super dealloc];
}
@end
readonly
readonly
value
MyInteger
@synthesize value;
@end
MyMutableInteger
@interface MyMutableInteger : MyInteger
@property(readwrite) NSInteger value;
@end
@implementation MyMutableInteger
@dynamic value;
- (void)setValue:(NSInteger)newX {
value = newX;
}
@end
retain
assign copy
nonatomic
// assign
property = newValue;
// retain
if (property != newValue) {
[property release];
property = [newValue retain];
}
// copy
if (property != newValue) {
[property release];
property = [newValue copy];
}
nonatomic
@synthesize
@synthesize
@synthesize noDeclaredIvar;
noDeclaredIvar
@interface
NSArray
NSArray
NSArray
NSArray
#import "ClassName.h"
@interface ClassName ( CategoryName )
// method declarations
@end
ClassName+CategoryName.m
#import "ClassName+CategoryName.h"
@implementation ClassName ( CategoryName )
// method definitions
@end
@private
NSArray
super
windowWillClose:
NSObject
NSWindow
NSObject
super
NSObject
self
NSObject
@implementation
NSNumber *number;
}
- (NSNumber *)number;
@end
@interface MyObject (Setter)
- (void)setNumber:(NSNumber *)newNumber;
@end
@implementation MyObject
- (NSNumber *)number {
return number;
}
@end
setNumber:
@interface
@interface MyObject : NSObject
{
NSNumber *number;
}
- (NSNumber *)number;
@end
@interface MyObject ()
- (void)setNumber:(NSNumber *)newNumber;
@end
@implementation MyObject
- (NSNumber *)number {
return number;
}
- (void)setNumber:(NSNumber *)newNumber {
number = newNumber;
}
@end
@interface
setNumber:
setNumber:
setNumber:
@implementation
@implementation
Type existingItem;
for ( existingItem in expression ) { statements }
NSFastEnumeration
statements
nil
NSEnumerator
NSFastEnumeration
NSArray NSDictionary
NSArray
NSEnumerator
NSDictionary
NSDictionary
NSSet
NSSet
NSManagedObjectModel
NSManagedObjectModel
NSArray
NSDictionary
NSEnumerator
NSArray *array = [NSArray arrayWithObjects:
@"One", @"Two", @"Three", @"Four", nil];
NSEnumerator *enumerator = [array reverseObjectEnumerator];
for (NSString *element in enumerator) {
if ([element isEqualToString:@"Three"]) {
break;
}
}
NSString *next = [enumerator nextObject];
// next = "Two"
NSArray
for
break
NSEnumerator
id
id
id
id
id
Rectangle *thisObject;
thisObject
id
id
id
display
thisObject
[thisObject display];
id
Shape
*aShape;
Rectangle *aRect;
aRect = [[Rectangle alloc] init];
aShape = aRect;
aRect
aShape
aShape
aRect
id
id
init
id
alloc
id
Rectangle *aRect;
aRect = [[Shape alloc] init];
NSObject
isFilled
[myRectangle display];
worry
- (double)worry;
double
- (int)worry;
worry
double
worry
worry
double
int
int
SEL
SEL
0
SEL
@selector()
setWidth:height:
setWidthHeight
SEL setWidthHeight;
setWidthHeight = @selector(setWidth:height:);
SEL
@selector()
NSSelectorFromString
setWidthHeight = NSSelectorFromString(aBuffer);
NSStringFromSelector
NSString *method;
method = NSStringFromSelector(setWidthHeight);
display
display
display
display
performSelector: performSelector:withObject:
performSelector:withObject:withObject:
NSObject
[friend performSelector:@selector(gossipAbout:)
withObject:aNeighbor];
[friend gossipAbout:aNeighbor];
id
helper = getTheReceiver();
SEL request = getTheSelector();
[helper performSelector:request];
helper
getTheReceiver
request
getTheSelector
SEL
performSelector:
id
NSControl
NSButtonCell
NSMatrix
NSButtonCell
NSButtonCell
NSButtonCell
[myButtonCell setAction:@selector(reapTheWind:)];
[myButtonCell setTarget:anObject];
NSObject performSelector:withObject:
id
NSButtonCell
NSButtonCell
respondsToSelector:
NSObject
if ( [anObject respondsToSelector:@selector(setOrigin::)] )
[anObject setOrigin:0.0 :0.0];
else
fprintf(stderr, "%s cant be placed\n",
[NSStringFromClass([anObject class]) UTF8String]);
respondsToSelector:
NSException NSError
-fobjc-exceptions
@catch()
@try
@catch()
@finally
@throw
NSException
@try
@catch()
@try
@catch()
@try {
...
}
@catch (CustomException *ce) { // 1
...
}
@catch (NSException *ne) { // 2
// Perform processing necessary at this level.
...
}
@catch (id ue) {
...
}
@finally { // 3
// Perform processing necessary whether an exception occurred
...
}
or not.
@throw exception;
@catch()
@throw
NSException
NSException
NSException
-fobjc-exceptions
@synchronized()
@synchronized()
@synchronized()
@synchronized()
self
self
self
- (void)criticalMethod
{
@synchronized(self) {
// Critical code.
...
}
}
initialize
@synchronized()
@synchronized()
Proxy
for
B
conformsToProtocol:
@protocol()
NSProxy
oneway
in
out
inout
bycopy
NSConnection
byref
- (BOOL)canDance;
canDance
Proxy
for
B
initial message
B
return information
oneway
- (oneway void)waltzAtWill;
oneway
oneway float
const
oneway id
oneway void
in
out
inout
inout
const
in
inout
in
inout
char *
in
out
out
inout
- danceWith:(id)aPartner;
danceWith:
id
aPartner
danceWith:
bycopy
- danceWith:(bycopy id)aClone;
bycopy
- (bycopy)dancer;
out
bycopy
byref
byref
bycopy
id
byref
bycopy
byref
foo
@Protocol foo
- (bycopy)array;
@end
foo
/* Hello.mm
* Compile with: g++ -x objective-c++ -framework Foundation Hello.mm
*/
#import <Foundation/Foundation.h>
class Hello {
private:
id greeting_text; // holds an NSString
public:
Hello() {
greeting_text = @"Hello, world!";
}
Hello(const char* initial_greeting_text) {
greeting_text = [[NSString alloc]
initWithUTF8String:initial_greeting_text];
}
void say_hello() {
printf("%s\n", [greeting_text UTF8String]);
}
};
@interface Greeting : NSObject {
@private
Hello *hello;
}
- (id)init;
- (void)dealloc;
- (void)sayGreeting;
- (void)sayGreeting:(Hello*)greeting;
-o hello
@end
@implementation Greeting
- (id)init {
if (self = [super init]) {
hello = new Hello();
}
return self;
}
- (void)dealloc {
delete hello;
[super dealloc];
}
- (void)sayGreeting {
hello->say_hello();
}
- (void)sayGreeting:(Hello*)greeting {
greeting->say_hello();
}
@end
int main() {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Greeting *greeting = [[Greeting alloc] init];
[greeting sayGreeting];
// > Hello,
// > Bonjour,
delete hello;
[greeting release];
[pool release];
return 0;
}
__cplusplus
__OBJC__
world!
monde!
@interface Foo {
class Bar { ... } // OK
}
@end
Bar *barPtr; // OK
@interface Foo {
struct CStruct { ... };
struct CStruct bigIvar; // OK
} ... @end
fobjc-call-cxx-cdtors
alloc
fobjc-call-cxx-cdtors
class_createInstance
dealloc
object_dispose
#import <Cocoa/Cocoa.h>
struct Class0 { void foo(); };
struct Class1 { virtual void foo(); };
struct Class2 { Class2(int i, int j); };
@interface
Class0
Class1
Class1
Foo : NSObject {
class0;
// OK
class1;
// ERROR!
*ptr;
// OKcall 'ptr = new Class1()' from Foo's init,
// 'delete ptr' from Foo's dealloc
Class2 class2;
// WARNING - constructor not called!
...
@end
this
this
BOOL
self
self
super
super
oneway
in out inout
bycopy
class
NSObject
class
[foo class]; // OK
class
NSObject *class; // Error
@interface
foo
@interface(foo)
id<someProtocolName> foo;
TemplateType<SomeTypeName> bar;
id
label: ::global_name = 3;
this
self
[receiver message]
self
super
objc/objc.h
id
Class
SEL
IMP
BOOL
id
YES
BOOL
id
NO
char
objc.h
nil
(id)0
Nil
(Class)0
NO
(BOOL)0
YES
(BOOL)1
#import
//
@interface
@implementation
@protocol
@end
@private
@protected
@public
#include
@protected
@try
@throw
@catch()
@try
@finally
@try
@property
@synthesize
@dynamic
@class
@selector(method_name)
@protocol(protocol_name)
@protocol
@encode(type_spec)
@"string"
NSString
NSString
@synchronized()
@interface
#import "ItsSuperclass.h"
@interface ClassName : ItsSuperclass < protocol_list >
{
instance variable declarations
}
method declarations
@end
#import "ClassName.h"
@implementation ClassName
method definitions
@end
#import "ClassName.h"
@interface ClassName ( CategoryName ) < protocol list >
method declarations
@end
#import "CategoryName.h"
@implementation ClassName ( CategoryName )
method definitions
@end
@protocol
@protocol ProtocolName <
declarations of required
@optional
declarations of optional
@required
declarations of required
@end
@optional
@required
@required
@protocol
@protocol ProtocolName;
@protocol()
oneway
in
out
inout
bycopy
byref
- (void)setWidth:(int)newWidth height:(int)newHeight
- (void)setWidthAndHeight:(int)newWidth :(int)newHeight
id
unsigned
int
unsigned int
self
_cmd
self
super
super
void
@interface SomeClass
-method __attribute__((deprecated));
self
@end
#include <AvailabilityMacros.h>
@interface SomeClass
-method DEPRECATED_ATTRIBUTE; // or some other deployment-target-specific macro
@end
.m
.h
initialize
conformsTo:
conformsToProtocol:
id
objc_ivar
objc_ivar_list
class_getInstanceMethod
class_getClassMethod
method_getArgumentInfo
NSData
NSView
@protocol
init...
self
init...
super
id
NSObject
NSObject
NSApplication
id
SEL
BOOL
bycopy
byref
//
@""
_cmd
__cplusplus
__OBJC__
.c
@catch()
alloc
allocWithZone:
Class
@class
self
@finally
.h
//
conformsToProtocol:
id
IMP
@implementation
#import
in
#include
#include
@encode()
@end
init
initialize
inout
#import
super
@interface
.mm
isa
isKindOfClass:
isMemberOfClass:
.m
Nil
nil
NO
NSClassFromString
NSSelectorFromString
NSStringFromSelector
oneway
out
performSelector:
performSelector:withObject:
performSelector:withObject:withObject:
SEL
@selector()
self
@private
@protected
@protocol
super
@public
@synchronized()
this
@throw
@try
respondsToSelector:
unsigned int
void
YES