Я унаследовал этот код:Objective-C: Почему бы не назвать назначенный инициализатор?
- (id)initWithLocation:(CLLocation *)inLocation {
if (self = [super init])
{
_location = [inLocation copy];
}
return self;
}
- (id)initWithLocation:(CLLocation *)inLocation offsetValue:(NSNumber *)offset {
if (self = [super init])
{
_location = [inLocation copy];
_offset = [offset copy];
}
return self;
}
и мне интересно, если есть хорошая причина, почему первый метод не вызывает обозначенный инициализатору (например, как этот Is it okay to call an init method in self, in an init method?)?
то почему бы не сделать это:
- (id)initWithLocation:(CLLocation *)inLocation {
if (self = [super init])
{
[self initWithLocation:inLocation offsetValue:nil];
}
return self;
}
- (id)initWithLocation:(CLLocation *)inLocation offsetValue:(NSNumber *)offset {
if (self = [super init])
{
_location = [inLocation copy];
_offset = [offset copy];
}
return self;
}
Учитывая, что ответы все одинаковы, я выбираю этот, так как он упоминает NS_DESIGNATED_INITIALIZER. – Snowcrash