不要让使用 - (id)init; 方法

Van*_*nel 0 objective-c init iphone-sdk-3.0

我正在为iPhone 3.1.3开发一款应用程序.

我有以下课程:

@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
Run Code Online (Sandbox Code Playgroud)

我不想让程序员使用,-(id)init;因为我需要在每次初始化时设置我的字段(形状,位置,边界).

我不想让程序员使用它:

Pattern* myPattern = [[Pattern alloc] init];
Run Code Online (Sandbox Code Playgroud)

我知道如何实现:

- (id) initWithNumShapes:(int)numShapes screenSize:(CGSize) screenSize{
    if (self = [super init]) {
        shapes = [NSMutableArray arrayWithCapacity:numShapes];
        locations = [NSMutableArray arrayWithCapacity:numShapes];
        bounds = screenSize;
    }
    return (self);
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Mat*_*uch 5

如果有人使用平原,则引发异常 init

- (id)init {
    [NSException raise:@"MBMethodNotSupportedException" format:@"\"- (id)init\" is not supported. Please use the designated initializer \"- (id)initWithNumShapes:screenSize:\""];
    return nil;
}
Run Code Online (Sandbox Code Playgroud)