为了使我所说的更清楚,这是一个例子.
有这个课程:
@interface Person : NSObject {
NSString *name;
}
@property (nonatomic, retain) NSString *name;
- (void)sayHi;
@end
Run Code Online (Sandbox Code Playgroud)
有了这个实现:
@implementation Person
@synthesize name;
- (void)dealloc {
[name release];
[super dealloc];
}
- (void)sayHi {
NSLog(@"Hello");
NSLog(@"My name is %@.", name);
}
@end
Run Code Online (Sandbox Code Playgroud)
在程序的某个地方,我这样做:
Person *person = nil;
//person = [[Person alloc] init]; // let's say I comment this line
person.name = @"Mike"; // shouldn't I get an error here?
[person sayHi]; // and here
[person release]; // and …Run Code Online (Sandbox Code Playgroud)