2012-05-04 5 views
1

나는 그런 식으로 정의 지정된 초기화와 클래스를 만들었습니다 initWithFrame 정의에서 예외를 throw하는 것에 대해 :지정 초기화 및 initWithFrame

- (id)initWithFrame:(CGRect)frame 
{ 
    @throw ([NSException exceptionWithName:@"InitError" 
            reason:@"This class needs to be initialized with initWithFrame:aProperty: method" 
            userInfo:nil]); 
} 

이 방법이 좋은 해결책입니까?

답변

4

aProperty의 기본값으로 지정된 초기화 프로그램을 호출하면 어떨까요?

+0

이 간단한 좋은 해결책 : – MatterGoal

+0

네, 그리고 그건 그냥'initWithFrame 호출하는 코드를 중단하지 않습니다'aProperty''에 대한 좋은 기본값은 무엇을 알고하지 않음으로써. 그리고 나중에 'aProperty'를 변경할 수있게되면 눈에 띄지 않습니다. – Cyrille

1

이니셜 라이저에는 두 가지 기술이 사용됩니다. 나는 거기에 더 확신한다.

기술 1 : 부동산의

- (id)initWithFrame:(CGRect)frame 
{ 
    return [self initWithFrame:frame aProperty:1.0f]; 
} 

기술 2의 기본값으로 초기화를 호출 initWithFrame에서

:

단순히 전무를 반환;

- (id)initWithFrame:(CGRect)frame 
{ 
    return nil; 
} 
관련 문제