无法将表达式的类型'()'转换为键入'UINavigationController?'
问题描述:
我无法解决AppDelegate.swift中的错误。无法将表达式的类型'()'转换为键入'UINavigationController?'
我收到一条消息'无法转换表达式的类型'()'来键入'UINavigationController?'
任何一个给我的建议?
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tabBarController = UITabBarController()
let alarmViewController = AlarmViewController(style: .Plain)
let recorderViewController = RecorderViewController()
let playViewController = PlayViewController()
let tabController1 = UINavigationController(rootViewController: recorderViewController)
tabController1?.tabBarItem = UITabBarItem(title: "Recorder", image: UIImage(named: "tabbar_microphone"), tag: 1)
let tabController2 = UINavigationController(rootViewController: playViewController)
tabController2?.tabBarItem = UITabBarItem(title: "Cheer me up!", image: UIImage(named: "tabbar_play"), tag: 2)
let tabController3 = UINavigationController(rootViewController: alarmViewController!)
tabController3?.tabBarItem = UITabBarItem(title: "Alarm", image: UIImage(named: "tabbar_alarm"), tag: 3)
******* here's a place I got the message 'Cannot convert the expression's type '()' to type 'UINavigationController?' *******
tabBarController.viewControllers = [tabController1, tabController2, tabController3]
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
答
你tabControllerX
s为所有自选项目(如你似乎意识到,当你分配UITabBarItem
■当引用它们全部为?
),所以你需要解开它们。最简单的方法,给你如何构建它,仅仅是改变线路
tabBarController.viewControllers = [tabController1!, tabController2!, tabController3!]
// Unwrap tabControllers
我会做不同的是,因为我不喜欢自选在我的逻辑不知道挂在他们是否是零或类似的东西
if let tabController1 = UINavigationController(rootViewController: recorderViewController)
// Now you know it's a tabController!
tabController1.tabBarItem = UITabBarItem(title: "Recorder", image: UIImage(named: "tabbar_microphone"), tag: 1)
// ...
} else {
// what are you going to do if it's nil?
}
真的很感谢您的回答! – 2014-10-01 05:26:32