如果我有一个名为轮胎的自定义类:
#import <Foundation/Foundation.h>
@interface Tires : NSObject {
@private
NSString *brand;
int size;
}
@property (nonatomic,copy) NSString *brand;
@property int size;
- (id)init;
- (void)dealloc;
@end
=============================================
#import "Tires.h"
@implementation Tires
@synthesize brand, size;
- (id)init {
if (self = [super init]) {
[self setBrand:[[NSString alloc] initWithString:@""]];
[self setSize:0];
}
return self;
}
- (void)dealloc {
[super dealloc];
[brand release];
}
@end
我在我的视图控制器中合成了一个 setter 和 getter:
#import <UIKit/UIKit.h>
#import "Tires.h"
@interface testViewController : UIViewController {
Tires *frontLeft, *frontRight, *backleft, *backRight;
}
@property (nonatomic,copy) Tires *frontLeft, *frontRight, *backleft, *backRight;
@end
====================================
#import "testViewController.h"
@implementation testViewController
@synthesize frontLeft, frontRight, backleft, backRight;
- (void)viewDidLoad {
[super viewDidLoad];
[self setFrontLeft:[[Tires alloc] init]];
}
- (void)dealloc {
[super dealloc];
}
@end
它在[self setFrontLeft:[[Tires alloc] init]]回来后死亡。它编译得很好,当我运行调试器时,它实际上一直通过轮胎上的init方法,但是一旦它回来,它就死了,视图永远不会出现。但是,如果我将viewDidLoad方法更改为:
- (void)viewDidLoad {
[super viewDidLoad];
frontLeft = [[Tires alloc] init];
}
它工作得很好。我可以放弃 setter 并直接访问frontLeft变量,但我的印象是我应该尽可能多地使用 setter 和 getter,从逻辑上讲,setFrontLeft方法似乎应该有效。
这带来了一个额外的问题,我的同事在这些方面一直在问(我们都是 Objective-C 的新手);如果您与那些 setter 和 getter 属于同一类,为什么还要使用 setter 和 getter。