gpt4 book ai didi

swift - Swift中修改引用类型参数;外面的变化是立即可见的吗?

转载 作者:行者123 更新时间:2023-11-28 15:45:27 25 4
gpt4 key购买 nike

我有一个 Swift Playground ,在源文件夹中包含多个类。这些类具有 tick() 函数,可通过 while true { } 尽快调用。我正在使用并行线程来执行此操作,以阻止 Xcode 锁定,代码如下:

DispatchQueue.global(qos: .background).async {
BackgroundThread.startMainLoop(scene: scene)
}

// other file
public struct BackgroundThread {
public static func startMainLoop (scene: SKScene) {
let songController = SongController()
let uiManager = UIManager(scene: scene)

// The UIManager is going to modify this scene.
// Will the changes be available to BackgroundThread and/or the main file?

while true {
songController.tick()
uiManager.tick()
}
}
}

因为主要的 playground 脚本非常慢,我已将重复循环移动到 Sources 文件夹中的已编译类。

我正在将一个 SKScene 对象传递给一个 UIManager 编译类,我需要在这个(相对复杂的)场景中比主要的 playground 脚本更快地更新 UI可以管理。

主 playground 脚本在并行线程中调用 startMainLoop() 函数。它传入 SKScene 对象。这个函数永远不会返回,因为它包含一个永无止境的循环。有没有一种方法可以修改传递的对象,以修改屏幕上显示的场景,实时,而无需返回函数(这意味着 inout 不是一个选项),假设默认情况下不会发生这种情况?

最佳答案

引用类型意味着该值是一个引用——即指向一个对象的指针。当你传递或分配一个引用时,你会得到一个引用的副本(它是没有 inout 的按值传递),但是引用的多个副本指向同一个对象,并且对通过指向它的任何引用的对象通过指向它的任何其他引用可见。

关于swift - Swift中修改引用类型参数;外面的变化是立即可见的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43149543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com