我想采取我的视图的维度,并将其分配给我的scrollView。这是我的代码:Swift optionals OptionalValue
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var screenRect:CGRect = self.window?.bounds
var bigRect = screenRect
bigRect.size.width *= 2.0
bigRect.size.height *= 2.0
var scrollView:UIScrollView = UIScrollView(frame: screenRect)
self.window?.addSubview(scrollView)
var hypnoView:View = View(frame: bigRect)
scrollView.addSubview(hypnoView)
scrollView.contentSize = bigRect.size
self.window?.backgroundColor = UIColor.whiteColor()
return true
}
当我写这篇文章的Xcode var screenRect:CGRect = self.window?.bounds
的Xcode给我一个错误OptionalValue .... I M接受到很多theese自选失去了什么?。 如何翻译旧的客观-c线
CGRect screenRect = self.window.bounds
转换成swift?
任何好的教程在可选?我读了一些,我得到了它们的用途,但是当它使用它们时,它从来没有出现在我的脑海里 – Marco 2014-11-08 19:22:24
答案有一个很大的错误:你建议使用强制展开'!',但是你会说:_ “窗户可能是零”_。如果可选项为零,则使用强制解包会导致运行时异常。应该使用可选绑定或显式检查nil。 – Antonio 2014-11-08 20:42:09
@Antonio:请阅读上面的讨论与nhgrif的答案。在这种情况下,当'self.window'为'nil'时,您特别*希望*您的代码大声崩溃。 – DPlusV 2014-11-08 20:55:22