可选类型'UIWindow的值?必须解包以引用已包装基本类型'UIWindow'的成员'viewWithTag'

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

我已将目标c文件转换为swift,现在将其转换为:

if isCaptured {

            let colourView = UIView(frame: window.frame)

            colourView?.backgroundColor = UIColor.black

            colourView?.tag = 1234

            colourView?.alpha = 0

            window!.makeKeyAndVisible()

            window.addSubview(colourView)

            // fade in the view

            UIView.animate(withDuration: 0.5, animations: {

                colourView?.alpha = 1

            })
        } else {

            // grab a reference to our coloured view

            let colourView = window.viewWithTag(1234)

            // fade away colour view from main view

            UIView.animate(withDuration: 0.5, animations: {

                colourView?.alpha = 0

            }) { finished in

                // remove when finished fading

                colourView?.removeFromSuperview()

            }
        }

但是我收到错误消息:“可选类型'UIWindow的值?必须解包以引用已包装基本类型'UIWindow'的成员'viewWithTag'

屏幕快照特此附上:error screenshot

如何纠正此问题。请提供任何帮助,谢谢

ios iphone xcode swift4.2 uiwindow
1个回答
0
投票

您已经用window!.makeKeyAndVisible()对其进行了包装。因此,您可以在这里-> window!.viewWithTag(1234)

也建议阅读https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html。您需要获得基本思想,才能知道强制展开的含义以及何时可以安全使用。

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