Declaration is only valid at file scope (extension)

大城市里の小女人 提交于 2019-12-04 05:02:53

问题


i try to make my app just in portrait mode iam using navigationcontroller i get this error iam usring Xcode7 and Swift 2 And Target System IOS 9.3
Declaration is only valid at file scope

        extension UINavigationController {
            public override func supportedInterfaceOrientations() -> Int {
                return visibleViewController.supportedInterfaceOrientations()
            }
            public override func shouldAutorotate() -> Bool {
                return visibleViewController.shouldAutorotate()
            }
        }

        extension UITabBarController {
            public override func supportedInterfaceOrientations() -> Int {
                if let selected = selectedViewController {
                    return selected.supportedInterfaceOrientations()
                }
                return super.supportedInterfaceOrientations()
            }
            public override func shouldAutorotate() -> Bool {
                if let selected = selectedViewController {
                    return selected.shouldAutorotate()
                }
                return super.shouldAutorotate()
            }
        }

Thnx


回答1:


The error message is quite clear. You cannot declare an extension inside of anything - inside a class declaration, inside a struct declaration, etc. It must be outside of everything, at the top level of the containing file. There must be no curly braces around it.

You have not shown the context in which you are declaring this extension, but clearly there are curly braces around it, or you wouldn't be getting the error!

But in any case, no matter where you declare them, your extensions are illegal: you can't do an override in an extension. (See my answer here.)



来源:https://stackoverflow.com/questions/38021452/declaration-is-only-valid-at-file-scope-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!