为什么 Xcode 说 'instance will be immediately deallocated'?

Why Xcode says that 'instance will be immediately deallocated'?

我试图找出为什么我在 Xcode 中收到以下消息:

Instance will be immediately deallocated because property 'delegate' is 'weak'

这是一个代码:

import ARKit
     
class OtherViewController: NSObject, ARSCNViewDelegate { }
    
class ViewController: UIViewController, ARSCNViewDelegate {
        
    var arView = ARSCNView(frame: .zero)
    let otherVC = OtherViewController()
        
    override func viewDidLoad() {
        super.viewDidLoad()

        // I'M USING ONE OF THESE
        
        arView.delegate = self                // works fine
        arView.delegate = otherVC             // works fine
        arView.delegate = ViewController()    // PRINTS WARNING
    }
}

问题:

我了解代表是 weak 和可选的:

weak var delegate: ARSCNViewDelegate? { get set }

但是,为什么 arView.delegate = selfarView.delegate = otherVC 工作正常但 arView.delegate = ViewController() 打印警告?

通过设置 arView.delegate = ViewController() 您正在创建一个 ViewController 的新实例,它不会保留在任何地方,所以它会在函数 returns.