Tires というカスタム クラスがある場合:
#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
そして、View Controller でセッターとゲッターを合成します。
#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]]が戻ってきた後に終了します。それは問題なくコンパイルされ、デバッガーを実行すると、実際にはTiresのinitメソッドを完全に通過しますが、戻ってくると、ただ死んでビューが表示されません。ただし、viewDidLoadメソッドを次のように変更すると:
- (void)viewDidLoad {
[super viewDidLoad];
frontLeft = [[Tires alloc] init];
}
それはうまく動作します。セッターを捨てて直接frontLeft変数にアクセスすることもできましたが、可能な限りセッターとゲッターを使用する必要があるという印象を受けました。論理的にはsetFrontLeftメソッドが機能するようです。
これは、私の同僚がこれらの点で尋ね続けている追加の質問をもたらします (私たちは皆、Objective-C を初めて使用します)。それらのセッターとゲッターと同じクラスにいる場合、なぜセッターとゲッターを使用するのですか。