私はObjective-Cにかなり慣れていないので、質問があります。
カスタムクラスを作成し、初期化用のオーバーロードを作成しようとしました。
- (id)init
{
if (self = [super init]) {
[self setIsCurrentCar:NO];
}
return self;
}
-(id) initWithID:(NSInteger)id {
if(self = [self init]) {
[self setID:id];
}
return self;
}
-(id) initWithID:(NSInteger)id CarYear:(NSString *)year {
if(self = [self initWithID:id]) {
[self setCarYear:year];
}
return self;
}
-(id) initWithIDCarYear
ある時点で、メソッドを呼び出したとしましょう。
上記のコードが構造的に正しいことを知りたいのですが。
- このコードで
self
は、3回に設定されています。より良い解決策はありますか? - このコードでメモリリークが発生しますか?(ARCを使用)
- 常にチェックする必要
if(self = ...)
がありますか、それとも冗長なコードですか?
ありがとうございました
@Edit 次のコードの方が良いですか?
-(id) initWithID:(NSInteger)id CarYear:(NSString *)year {
if (self = [super init]) {
[self setIsCurrentCar:NO];
[self setID:id];
[self setCarYear:year];
}
return self;
}