访问UIapplication's主窗口时,它将作为a返回UIWindow??
let view = UIApplication.sharedApplication().delegate?.window // view:UIWindow??
Run Code Online (Sandbox Code Playgroud)
为什么它作为双重可选项返回,它是什么意思,如果放入一个if let应该添加一个!之后?
if let view = UIApplication.sharedApplication().delegate?.window!
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是?用!后代表代替,但那不是解决方案.
如何设置协议的可选属性?例如,UITextInputTraits具有许多可选的读/写属性.当我尝试以下操作时,我收到编译错误(无法在'textInputTraits'中分配给'keyboardType'):
func initializeTextInputTraits(textInputTraits: UITextInputTraits) {
textInputTraits.keyboardType = .Default
}
Run Code Online (Sandbox Code Playgroud)
通常在访问协议的可选属性时添加问号,但在分配值时这不起作用(错误:无法分配给此表达式的结果):
textInputTraits.keyboardType? = .Default
Run Code Online (Sandbox Code Playgroud)
协议看起来像:
protocol UITextInputTraits : NSObjectProtocol {
optional var keyboardType: UIKeyboardType { get set }
}
Run Code Online (Sandbox Code Playgroud) 在观看Apple关于LLDB调试器的视频时,我发现了一些我无法找到解释的内容; 当他写道时,他正在谈论可选值:
var optional: String? = nil; //This is ok, a common optional
var twice_optional: String?? = nil; //What is this and why's this useful??
Run Code Online (Sandbox Code Playgroud)
我打开了一个游乐场并开始尝试它,并意识到你可以根据需要编写任意?数量的游戏,然后用相同的数量打开它们!.我理解包装/解包变量的概念,但不能想到我想要包装值4,5或6次的情况.
我正在尝试更新PKHUD(https://github.com/pkluz/PKHUD)以使用Xcode 6 beta 5,除了一个小细节之外我几乎要通过:
internal class Window: UIWindow {
required internal init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
internal let frameView: FrameView
internal init(frameView: FrameView = FrameView()) {
self.frameView = frameView
// this is the line that bombs
super.init(frame: UIApplication.sharedApplication().delegate.window!.bounds)
rootViewController = WindowRootViewController()
windowLevel = UIWindowLevelNormal + 1.0
backgroundColor = UIColor.clearColor()
addSubview(backgroundView)
addSubview(frameView)
}
// more code here
}
Run Code Online (Sandbox Code Playgroud)
Xcode给了我错误UIWindow? does not have a member named 'bounds'.我很确定这是一个与打字有关的微不足道的错误,但我几个小时都找不到答案.
此外,此错误仅发生在Xcode 6 beta 5中,这意味着答案在于Apple最近更改的内容.
非常感谢所有帮助.