更新inout param异步不会更新引用

时间:2016-10-04 22:02:39

标签: swift asynchronous pass-by-reference grand-central-dispatch inout

使用下面的游乐场,当我在异步中打印时,我会假设计数应该相同,但它们不是。似乎swift没有正确地异步使用inout,但我需要能够在异步调用中更新inout引用。知道如何使这项工作吗?

import XCPlayground
import Foundation
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true


class MyObject {

    var name:String!
    var id:Int!

    init(name:String, id:Int){
        self.name = name
        self.id = id
    }

}

struct TempStorage {
    static var baseObjs:[MyObject] = []
}

class myUtils {

    private static func myFunc2(inout myObjs:[MyObject]){

        let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
        dispatch_async(dispatch_get_global_queue(priority, 0)) {
            let newObj = MyObject(name: "BOB", id: 1)
            myObjs = [newObj]
            print(myObjs.count)
            print(TempStorage.baseObjs.count)
        }


    }

    static func myFunc(inout myObjs:[MyObject]) {
        myUtils.myFunc2(&myObjs)
    }
}

var baseObj = MyObject(name:"SUSY", id:0)
print(TempStorage.baseObjs.count)
myUtils.myFunc(&TempStorage.baseObjs)

0 个答案:

没有答案