2015-02-23 97 views
1

我下载了Apple's Table Search with UISearchController (Obj-C and Swift)示例代码。'UIWindow?'没有名为'rootViewController'的成员

我正在使用Xcode 6.3测试版。打开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 
    } 
} 

回答

3

试试这个,你可以访问之前初始化它:

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 
} 

我希望这可以帮助您

+0

这些行是否属于属性声明? – Adrian 2015-02-23 20:06:19

+0

@AdrianB把'window = UIWindow(frame:UIScreen.mainScreen()。bounds)'放在'let navController = window!.rootViewController as! UINavigationController',它必须正常工作 – 2015-02-23 20:08:33

+0

你的'应用程序:didFinishLaunchingWithOptions:'功能 – 2015-02-23 20:11:05