我正试图用迅捷的语言来表达我的 idea .在使用Objective-C的代码中构建视图时,一种常见的模式是覆盖UI属性,并像这样延迟加载它们:
@property(nonatomic, strong) UILabel *myLabel;
- (UILabel *)myLabel
{
if (!_myLabel) {
_myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 75.0f, 320.0f, 20.0f)];
[_myLabel setFont:[UIFont subHeadlineFont]];
[_myLabel setTextColor:[UIColor subHeadlineColor]];
[_myLabel setText:@"Hello World"];
}
return _myLabel;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.myLabel];
}
这允许UIElements的配置在其设置中自包含,但不会导致每次都重新配置它们.
我们似乎无法访问Swift中的备份存储,而且@lazy
关键字的语义也不完全相同.
我很好奇,是否有人在Swift中发现了一种类似的模式,允许人们以简洁的语法方式保留变量和常量的配置及其声明,而不是每次都导致重新配置?