我正在爲iPhone 3.1.3開發一個應用程序。不要讓 - - (id)init;方法
我有下面的類:
@interface Pattern : NSObject {
NSMutableArray* shapes;
NSMutableArray* locations;
CGSize bounds;
}
@property (nonatomic, retain, readonly) NSMutableArray* shapes;
@property (nonatomic, retain, readonly) NSMutableArray* locations;
- (id) initWithNumShapes:(int)numShapes screenSize:(CGSize)screenSize;
- (void) addObject:(Object2D*) newObject;
@end
我不想讓程序員使用-(id)init;
,因爲我需要設置我的字段(形狀,位置,邊界)上的每個初始化。
我不想讓程序員使用這樣:
Pattern* myPattern = [[Pattern alloc] init];
我知道如何實現:
- (id) initWithNumShapes:(int)numShapes screenSize:(CGSize) screenSize{
if (self = [super init]) {
shapes = [NSMutableArray arrayWithCapacity:numShapes];
locations = [NSMutableArray arrayWithCapacity:numShapes];
bounds = screenSize;
}
return (self);
}
我怎麼能這樣做?
好的答案!非常感謝! – VansFannel