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

发布于 2024-11-17 23:50:14 字数 968 浏览 9 评论 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

我不想让程序员使用 -(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);
}

我该怎么做?

I'm developing an app for iPhone 3.1.3.

I have the following class:

@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

I don't want to let programmers use -(id)init; because I need to setup my fields (shape, locations, bounds) on every initialization.

I don't want to let programmers use this:

Pattern* myPattern = [[Pattern alloc] init];

I know how to implement:

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

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

有木有妳兜一样 2024-11-24 23:50:14

如果有人使用普通的 init 则引发异常

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

raise an exception if somebody uses the plain init

- (id)init {
    [NSException raise:@"MBMethodNotSupportedException" format:@"\"- (id)init\" is not supported. Please use the designated initializer \"- (id)initWithNumShapes:screenSize:\""];
    return nil;
}
和影子一齐双人舞 2024-11-24 23:50:14

如果您有以下情况,您可以重写 init 函数并为其提供默认值:

- (id)init {
    return [self initWith....];
}

如果您根本不需要 init,仍然重写并抛出某种异常,表示不使用 init。

- (id)init {
    NSAssert(NO, @"Please use other method ....");
    return nil;
}

如果有人尝试调用init,这总是会给出异常。

我建议使用前一种情况,并有一些默认值。

You can override the init function and give default values from it if you have:

- (id)init {
    return [self initWith....];
}

If you don't want init at all, still override and throw some kind of exception saying not to use init.

- (id)init {
    NSAssert(NO, @"Please use other method ....");
    return nil;
}

This will always give an exception if anyone tried to call init.

I would suggest to use the former case though, and have some default values.

尬尬 2024-11-24 23:50:14

它始终是相同的架构。只需在您的超类(NSObject)上调用 init 即可。

- (id) initWithNumShapes:(int)numShapes screenSize:(CGSize)screenSize {
     if(self == [super init]) {
          // Custom Init your properties
          myNumShapes = numShapes;
          myScreenSize = screenSize;
     }
     return self;
}

Its always the same schema. Just call init on your superclass (NSObject).

- (id) initWithNumShapes:(int)numShapes screenSize:(CGSize)screenSize {
     if(self == [super init]) {
          // Custom Init your properties
          myNumShapes = numShapes;
          myScreenSize = screenSize;
     }
     return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文