gpt4 book ai didi

objective-c - 指定初始化器并调用它

转载 作者:行者123 更新时间:2023-11-29 11:13:30 24 4
gpt4 key购买 nike

我有关于指定初始化程序的一般性问题。我有一个类,我想从那里调用一个初始化程序,但在我开始用传递的数据填充我的@properties 之前,我想将数据设置为默认值。例如:

-(id)initWithDefault:(NSDictionary*)defaultTemplate
{
self = [super init];

_fontColor = [defaultTemplate objectForKey:@"color"];
_fontSize = [[defaultTemplate objectForKey:@"size"] intValue];

return self;
}

-(id)initWithTemplate:(NSDictionary*)template
{
self = [self initWithDefault:myDefaultTemplate];

//now i doing something with my template

return self;

}

这是一种防止 null @properties 的方法吗?这是对指定初始值设定项的正确使用吗?当然你可以假设 myDefaultTemplates 不为空,并且在键中没有空对象。

最佳答案

这对我来说似乎很好。我会使用安全的方式(如下所示),但除此之外您的代码没问题。

-(id)initWithTemplate:(NSDictionary*)template 
{
if(self = [self initWithDefault:myDefaultTemplate]) {

//now i doing something with my template

}

return self;

}

关于objective-c - 指定初始化器并调用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10496429/

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