声明仅在文件范围内有效(扩展名)

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

我试图让我的应用程序只是在纵向模式我使用navigationcontroller我得到这个错误我使用Xcode7Swift 2和目标系统IOS 9.3 声明仅在文件范围内有效

        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

swift swift2 ios9 xcode7
1个回答
9
投票

错误信息非常清楚。你不能在extension声明中,在class声明中声明struct内部任何东西 - 它必须在所有内容之外,在包含文件的顶层。它周围一定没有花括号。

你没有显示你声明这个extension的上下文,但显然它周围有花括号,或者你不会得到错误!

但无论如何,无论你在哪里声明它们,你的扩展都是非法的:你不能在扩展中做override。 (见我的回答here。)

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