3

I downloaded Apple's Table Search with UISearchController (Obj-C and Swift) sample code.

I'm using Xcode 6.3 beta. Upon opening the Swift file, I converted the code to Swift 1.2 (via Edit/Convert). After conversion, I get the following compiler errors in AppDelegate.swift (I've also noted the lines where the errors occur in the the raw code below.:

 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'`

Any thing jump out? Here's 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
    }
}
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
Adrian
  • 16,233
  • 18
  • 112
  • 180

1 Answers1

4

Try this, you have to initialize before access it :

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
}

I hope this help you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • Do those lines go in the property declarations? – Adrian Feb 23 '15 at 20:06
  • @AdrianB put the `window = UIWindow(frame: UIScreen.mainScreen().bounds)` just before `let navController = window!.rootViewController as! UINavigationController` and it has to work fine – Victor Sigler Feb 23 '15 at 20:08
  • Inside of your `application:didFinishLaunchingWithOptions:` function – Victor Sigler Feb 23 '15 at 20:11
  • Thanks. Doesn't seem to be working. I threw them in `didFinishLaunchingWithOptions`. This is Xcode 6.3, so I suspect that's part of the problem. – Adrian Feb 23 '15 at 20:24
  • @AdrianB I have the same code in Xcode 6.3 and it's working fine wihout errors – Victor Sigler Feb 23 '15 at 20:26
  • No idea what's causing it. I got rid of one error, but I still get `Objective-C method 'application:didFinishLaunchingWithOptions:' provided by method 'application(_:didFinishLaunchingWithOptions:)' conflicts with optional requirement method 'application(_:didFinishLaunchingWithOptions:)' in protocol 'UIApplicationDelegate'` after adding your fix. I've cut and pasted from here. I closed Xcode, nuked caches "the old fashioned" way & I still get it. Thanks for your help. – Adrian Feb 23 '15 at 20:41
  • 1
    Try this http://stackoverflow.com/questions/28460472/objective-c-method-conflicts-with-optional-requirement-method-swift – Victor Sigler Feb 23 '15 at 20:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71526/discussion-between-victor-sigler-and-adrian-b). – Victor Sigler Feb 23 '15 at 20:45