gpt4 book ai didi

objective-c - 指定的初始化程序和initWithFrame

转载 作者:行者123 更新时间:2023-12-01 18:00:40 25 4
gpt4 key购买 nike

我用指定的初始值设定项创建了一个类,方法如下:

-(id)initWithFrame:(CGRect)frame 
aProperty:(float)value {

self = [super initWithFrame:frame];

if(self){
///....do something
}
}

现在,我想知道如何避免直接调用initWithFrame方法,例如:
MyClass *theObj = [MyClass alloc]initWithFrame:theFrame];

我考虑过在initWithFrame定义中引发异常:
- (id)initWithFrame:(CGRect)frame
{
@throw ([NSException exceptionWithName:@"InitError"
reason:@"This class needs to be initialized with initWithFrame:aProperty: method"
userInfo:nil]);
}

这是一个好的解决方案吗?

最佳答案

如何使用aProperty的默认值调用指定的初始化程序呢?

关于objective-c - 指定的初始化程序和initWithFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10445531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com