2017-04-24 68 views
0

今天,我在腦海中浮現了一個問題,在很長一段時間後再次使用客觀的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中編寫相同的代碼?

更新:我已經知道使用延時實例的方式。有沒有其他方法可以創建不可變實例?

+4

一類C結構是一個「複合語句表達」有些事情,看http://stackoverflow.com/questions/21909511/ios-name-of-this-way例如,構建和返回一個對象在目標c。 –

+0

@MartinR - 我可以在這裏使用相同的東西嗎? – appleBoy21

回答

3

我使用這樣

@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

相關問題