声明仅在文件范围内有效(扩展名) [英] Declaration is only valid at file scope (extension)

查看:192
本文介绍了声明仅在文件范围内有效(扩展名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Navigationcontroller使我的应用程序仅处于纵向模式iam 我收到使用Xcode7Swift 2以及目标系统IOS 9.3
的错误 声明仅在文件范围内有效

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

推荐答案

错误消息非常清楚.您不能在任何内容内部声明extension-在class声明内部,在struct声明内部等.它必须在所有内容之外,在包含文件的顶层.周围不能有花括号.

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.

您尚未显示要声明此extension的上下文,但显然它周围有 大括号,否则您将不会得到错误提示!

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!

但是无论如何,无论您在何处声明它们,您的扩展名都是非法的:您不能在扩展名中使用override. (请参见我的答案此处.)

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.)

这篇关于声明仅在文件范围内有效(扩展名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆