objective-c - oc这段代码转化为swift 应该怎么写的?
给我你的怀抱
给我你的怀抱 2017-05-02 09:20:56
0
4
637
- (NSMutableArray *)orderArray {
    if (!_orderArray) {
        _orderArray = [NSMutableArray array];
    }
    return _orderArray;
}
给我你的怀抱
给我你的怀抱

reply all(4)
过去多啦不再A梦

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 的类型会变为 NSArray,
// 而不是 Swift 的类型,也不是 NSMutableArray
lazy var orders = []

(PS: orders is a better name)

小葫芦
lazy var orderArray = NSMutableArray()
大家讲道理

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 }()

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template