- (NSMutableArray *)orderArray {
if (!_orderArray) {
_orderArray = [NSMutableArray array];
}
return _orderArray;
}
If you are correct, it should be the attribute get method in Objective-C.
Actually, @Tr2e was almost completely correct. His answer could have been improved with a slight modification:
lazy var orders: [AnyObject] = []
NO:
// 如果省略 `:` 后面的類型的話,orders 的類型會變?yōu)?NSArray,
// 而不是 Swift 的類型,也不是 NSMutableArray
lazy var orders = []
(PS: orders is a better name)
Single case?
If hard translation
func orderArray()->NSMutableArray{
if _orderArray != nil {
_orderArray = NSMutableArray.array()
}
return _orderArray
}
Swift point
// 單例類
class SharedObject {
static let sharedInstance = SharedObject()
}
// 使用
SharedObject.sharedInstance.doSomething()
There is a keyword called lazy, which is roughly like this:
lazy var array = { let a = NSArray.init(); return a }()