0

使用下面的操场,当我在异步中打印时,我会假设计数应该相同,但事实并非如此。似乎 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)
4

0 回答 0