如何在不使用Storyboard的情况下创建新的Swift项目? [英] How do I create a new Swift project without using Storyboards?
问题描述
在XCode 6中创建新项目不允许禁用Storyboard。您只能选择Swift或Objective-C并使用或不使用核心数据。
Creating a new project in XCode 6 doesn't allow to disable Storyboards. You can only select Swift or Objective-C and to use or not Core Data.
我尝试删除故事板并从项目中删除主故事板并手动设置窗口来自didFinishLaunching
I tried deleting the storyboard and from the project removing the main storyboard and manually setting the window from didFinishLaunching
在AppDelegate中我有这个:
In the AppDelegate I have this:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow
var testNavigationController: UINavigationController
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
testNavigationController = UINavigationController()
var testViewController: UIViewController = UIViewController()
self.testNavigationController.pushViewController(testViewController, animated: false)
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window.rootViewController = testNavigationController
self.window.backgroundColor = UIColor.whiteColor()
self.window.makeKeyAndVisible()
return true
}
}
然而,XCode给了我一个错误:
However, XCode gives me an error:
Class'AppDelegate'没有初始值设定项
Class 'AppDelegate' has no initializers
任何人都成功了吗?
推荐答案
你必须标记 window
和 testNavigationController
变量为可选:
You must mark the window
and testNavigationController
variables as optional:
var window : UIWindow?
var testNavigationController : UINavigationController?
Swift类需要在实例化期间初始化非可选属性:
Swift classes require non-optional properties to be initialized during the instantiation:
在创建该类或结构的实例时,类和结构必须将其所有存储属性设置为适当的初始值。存储的属性不能保留在不确定的状态。
Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.
可选类型的属性会自动初始化为值为nil,表示该属性是故意没有值但是在初始化期间。
Properties of optional type are automatically initialized with a value of nil, indicating that the property is deliberately intended to have "no value yet" during initialization.
使用可选变量时,请记得用打开它们!
,例如:
When using optional variables, remember to unwrap them with !
, such as:
self.window!.backgroundColor = UIColor.whiteColor();
这篇关于如何在不使用Storyboard的情况下创建新的Swift项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!