//string1 将被自动释放

NSString* string1 = [NSString string];


NSString* string2 = [[NSString alloc] init]; [string2 release];

Typically, each class gets two files: a header file that contains the @interface for the class and a dot-m file that holds the @implementation.






一个实例变量的设置器(setter)会自动释放(autorelease)原来引用的对象,同时保留(retain)新的。你只需要保证在dealloc函数中释放 (release)了它就行了。

  • When you create an object using new, alloc, or copy, the object has a retain count of 1. You are responsible for sending the object a release or autorelease message when you’re done with it. That way, it gets cleaned up when its useful life is over.
  • When you get hold of an object via any other mechanism, assume it has a retain count of 1 and that it has already been autoreleased. You don’t need to do any further work to make sure it gets cleaned up. If you’re going to hang on to the object for any length of time, retain it and make sure to release it when you’re done.
  • If you retain an object, you need to (eventually) release or autorelease it. Balance these retains and releases.

如果过你通过alloc或者copy创建了一个对象,在函数结尾的地方给它发送一个release或者autorelease消息就行了。如果你是通过其它方式创建的对象,就什么也别做。“If I get it from new, alloc, or copy, I have to release or autorelease it.”


@property (retain) NSString* caption;

@property 是Objective-C语言的一个指令,通过它声明属性。带括号的”retain”指示设置器(setter)要保留输入值,该行后面的是指定属性的类型以及名称。

@synthesize 指令为我们主动生成了setter和getter.

@class sets up a forward reference. This is a way to tell the compiler, “Trust me; you’ll learn eventually what this class is, but for now, this is all you need to know.”

The colored boxes next to the name indicate what the symbol is: E for an enumerated symbol, f for a function, # for a #define, m for a method, C for a class, and so on.

Chose File-Make Snapshot (or its handy shortcut, Command-Control-S) and Xcode will remember the state of your project.