如何在Objective C中编写计算初始值设定项?
问题描述:
今天,我在脑海中浮现了一个问题,在很长一段时间后再次使用客观的c。可以说我有用swift 3.0编写的计算初始值设定项的代码。如何在Objective C中编写计算初始值设定项?
private let loginButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(hex: 0x0083C5, alpha: 1)
button.setTitle("Login", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .bold)
button.layer.cornerRadius = 5
return button
}()
以上代码返回的UIButton的一个实例。我如何在目标C中编写相同的代码?
更新:我已经知道使用延时实例的方式。有没有其他方法可以创建不可变实例?
答
我使用这样
@interface TestClass : NSObject
@property (nonatomic, readonly) UIButton* button;
@end
@implementation TestClass
@synthesize button=_button;
-(UIButton*)button
{
if (_button==nil) {
_button = [UIButton buttonWithType:UIButtonTypeSystem];
...
}
return _button;
}
@end
+0
感谢da post – appleBoy21
+0
不客气 – Sergey
一类C结构是一个“复合语句表达”有些事情,看http://stackoverflow.com/questions/21909511/ios-name-of-this-way例如,构建和返回一个对象在目标c。 –
@MartinR - 我可以在这里使用相同的东西吗? – appleBoy21