【发布时间】:2014-09-07 12:19:28
【问题描述】:
我最近将我的 Xcode 6 beta 6 更新为 Xcode 6 beta 7,突然我的部分代码无法编译。我的代码中有这个函数,它在if let layoutManager = textView.layoutManager 行上给出了错误条件绑定中的绑定值必须是可选类型。
func textTapped(recognizer: UITapGestureRecognizer){
self.textHasBeenTapped = true
if let textView = recognizer.view as? UITextView{
if let layoutManager = textView.layoutManager {
// rest of code
}
}
我尝试将 textView 设置为如下所示的可选类型(它消除了初始错误),但它却给出了错误 Value of optional type 'CGFloat?'未拆封;你的意思是用'!'或“?”? 在location.x = textView?.textContainerInset.left 行。如果我在left 之后插入! 或?,它反而会给我错误:后缀的操作数'!'应该有可选类型; type 是 'CGFloat' 建议我应该删除任何一个 '!'或者 '?'从而形成一种错误循环。
func textTapped(recognizer: UITapGestureRecognizer){
self.textHasBeenTapped = true
if let textView: UITextView? = recognizer.view as? UITextView{
if let layoutManager = textView?.layoutManager {
var location: CGPoint = recognizer.locationInView(textView)
location.x = textView?.textContainerInset.left
// rest of code
}
}
}
解决此问题的最佳方法是什么?
【问题讨论】:
标签: ios xcode swift optional beta