search

Home  >  Q&A  >  body text

objective-c - oc这段代码转化为swift 应该怎么写的?

- (NSMutableArray *)orderArray {
    if (!_orderArray) {
        _orderArray = [NSMutableArray array];
    }
    return _orderArray;
}
给我你的怀抱给我你的怀抱2757 days ago601

reply all(4)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-02 09:22:56

    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)

    reply
    0
  • 高洛峰

    高洛峰2017-05-02 09:22:56

    lazy var orderArray = NSMutableArray()

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-02 09:22:56

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

    reply
    0
  • PHP中文网

    PHP中文网2017-05-02 09:22:56

    There is a keyword called lazy, which is roughly like this:
    lazy var array = { let a = NSArray.init(); return a }()

    reply
    0
  • Cancelreply