我的代码NSimage()在Swift 3中工作正常,但它在Swift 5中不起作用

问题描述 投票:-1回答:1

我的代码是在窗口中显示image.jpg。这是用Swift 3编写的。最近,我用Xcode 10.2更新了Swift 4。它根本不起作用。我也在Swift 5中重写了它。它仍然不起作用。我不得不下载旧的Xcode 10,以便在Swift 3中运行我的代码再次工作。我想知道代码发生了什么。

这是我显示图片的主要代码。

override func viewDidAppear() {
    super.viewDidAppear()

    imageView.image = NSImage(byReferencingFile: "\(ViewController.GlobaVariable.selecFile)" )

    print("\(ViewController.GlobaVariable.selecFile)")
    /// The print statement work  but not the image.
}
swift cocoa nsimage
1个回答
-1
投票
    let openPanel = NSOpenPanel()
    openPanel.beginSheetModal(for:self.view.window!) { (response) in
        if response.rawValue == NSFileHandlingPanelOKButton
        {
            let fileURL = openPanel.url!

            //////////////////
            let ph = fileURL.absoluteString
            let path = (ph as NSString).substring(from: 7)
            //print(path)

            //////////////////////

            GlobaVariable.selecFile = path

        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.