Sea*_*ken 2 macos initialization objective-c ios
假设我有一个指定的初始化程序,它执行一些初始化:
- (id)initWithBlah:(NSString *)arg1 otherBlah:(NSArray *)arg2
{
if (self = [super init])
{
...
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
我有另一个需要调用它的初始化程序,但然后执行一些其他设置任务:
- (id)initWithSomeOtherBlah:(void *)creativeArg
{
// Is this right? It seems to compile and run as expected, but feels wrong
self = [self initWithBlah:nil otherBlah:nil];
if (self)
{
[self someProcessingForThisInitDependentOnSelfInit:creativeArg];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
由于测试以确保返回值是正确的,在这种情况下应该"自我"使用吗?我想知道这是否是一个有效的事件组合.我们有一种情况,我们有一个初始化程序,需要在运行指定的初始化程序后执行一些额外的设置.
我想知道是否正确的方法是在指定的初始化程序中推送这个额外的处理.
如果需要进一步澄清,请告诉我.我试图保持这个简单.:)
谢谢!
我遵循的一般经验法则是指定的初始化程序是具有最多参数的初始化程序,而其他初始化程序链接到指定的初始化程序.
在您的示例中,您没有在initWithSomeOtherBlah构造函数中使用creativeArg.我不确定这是否是故意的.
使用这种方法,在创建对象而不是副作用编程时,您明确表达了自己的意图.
例如:
@implementation BlaClass
- (id)initWithBlah:(NSString *)arg1 otherBlah:(NSArray *)arg2 creativeArg:(void *)arg3
{
if (self = [super init])
{
self.arg1 = arg1;
self.arg2 = arg2;
self.arg3 = arg3;
[self someProcessingForThisInitDependentOnSelfInit:arg3];
}
return self;
}
- (void)someProcessingForThisInitDependentOnSelfInit:(void *)creativeArg
{
if(creativeArg == NULL) return;
//do creative stuff
}
- (id)initWithSomeOtherBlah:(void *)arg
{
return [self initWithBlah:nil otherBlah:nil creativeArg:arg];
}
...
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
730 次 |
| 最近记录: |