"UIWindow?"没有名为"rootViewController"的成员。

3

我下载了Apple's Table Search with UISearchController (Obj-C and Swift)示例代码。

我正在使用Xcode 6.3 beta。打开Swift文件后,我通过编辑/转换将代码转换为Swift 1.2。转换后,在AppDelegate.swift中出现以下编译器错误(我也在下面的原始代码中注明了出现错误的行数):

 Objective-C method 'application:didFinishLaunchingWithOptions:' provided by method 'application(_:didFinishLaunchingWithOptions:)' conflicts with optional requirement method 'application(_:didFinishLaunchingWithOptions:)' in protocol 'UIApplicationDelegate'

'UIWindow?' does not have a member named 'rootViewController'`

有什么问题突出吗?这是AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    // MARK: Properties

    var window: UIWindow?

    // MARK: Application Life Cycle

    // error on the line below
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

        let products = [
            Product(type: Product.deviceTypeTitle, name: "iPhone", year: 2007, price: 599.00),
            Product(type: Product.deviceTypeTitle, name: "iPod", year: 2001, price: 399.00),
            Product(type: Product.deviceTypeTitle, name: "iPod touch", year: 2007, price: 210.00),
            Product(type: Product.deviceTypeTitle, name: "iPad", year: 2010, price: 499.00),
            Product(type: Product.deviceTypeTitle, name: "iPad mini", year: 2012, price: 659.00),
            Product(type: Product.desktopTypeTitle, name: "iMac", year: 1997, price: 1299.00),
            Product(type: Product.desktopTypeTitle, name: "Mac Pro", year: 2006, price: 2499.00),
            Product(type: Product.portableTypeTitle, name: "MacBook Air", year: 2008, price: 1799.00),
            Product(type: Product.portableTypeTitle, name: "MacBook Pro", year: 2006, price: 1499.00)
        ]

        // error on the line below
        let navController = window.rootViewController as! UINavigationController

        // Note we want the first view controller (not the visibleViewController) in case
        // we are being store from UIStateRestoration.
        let tableViewController = navController.viewControllers[0] as! MainTableViewController
        tableViewController.products = products

        return true
    }

    // MARK: UIStateRestoration

    func application(application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
        return true
    }

    func application(application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
        return true
    }
}
1个回答

4

尝试这个,你必须在访问之前初始化:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

    // declaration of products like in your above code
    ....

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let navController = window!.rootViewController as! UINavigationController           

    let tableViewController = navController.viewControllers[0] as! MainTableViewController
    tableViewController.products = products

    return true
}

我希望这可以帮助您。

这些行代码要放在属性声明里吗? - Adrian
@AdrianB 在 let navController = window!.rootViewController as! UINavigationController 之前加上 window = UIWindow(frame: UIScreen.mainScreen().bounds),这样就可以正常工作了。 - Victor Sigler
在你的 application:didFinishLaunchingWithOptions: 函数内部 - Victor Sigler
不知道是什么原因导致的。我解决了一个错误,但在添加你的修复后,我仍然得到了“Objective-C方法'application:didFinishLaunchingWithOptions:'由方法'application(_:didFinishLaunchingWithOptions :) '提供与协议'UIApplicationDelegate'中的可选要求方法'application(_:didFinishLaunchingWithOptions :)' 冲突”的错误。我已经从这里剪切和粘贴了。我关闭了Xcode,以“老派”的方式清除了缓存,但我仍然遇到了这个问题。感谢您的帮助。 - Adrian
1
尝试这个:https://dev59.com/IF4b5IYBdhLWcg3w-1-c - Victor Sigler
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接