我正在尝试使用Swift 2在另一个控制器中加载MyChatController。此代码在Swift 1.2中运行良好但在更新到Xcode 7后,我的应用程序崩溃并出现以下错误:
致命错误:在解包可选值时意外发现nil
有没有人知道如何解决Swift 2中的这个问题?
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let chatController: MyChatController = MyChatController()
chatController.opponentImage = UIImage(named: "User")
chatController.title = "My Chat"
let helloWorld = ChatMessage(content: "Hello....!!", sentBy: .User)
chatController.messages = [helloWorld]
chatController.delegate = self
chatController.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(chatController, animated: false)
}
答案 0 :(得分:0)
我已经完成了你的代码。 有一个错误。请完成以下步骤。
因此,如果self.selectedTextRange为nil,则返回方法的开头。
func align(){
if self.selectedTextRange == nil {
return
}
let caretRect: CGRect = self.caretRectForPosition(self.selectedTextRange!.end)
let topOfLine = CGRectGetMinY(caretRect)
let bottomOfLine = CGRectGetMaxY(caretRect)
let contentOffsetTop = self.contentOffset.y
let bottomOfVisibleTextArea = contentOffsetTop + CGRectGetHeight(self.bounds)
/*
If the caretHeight and the inset padding is greater than the total bounds then we are on the first line and aligning will cause bouncing.
*/
let caretHeightPlusInsets = CGRectGetHeight(caretRect) + self.textContainerInset.top + self.textContainerInset.bottom
if caretHeightPlusInsets < CGRectGetHeight(self.bounds) {
var overflow: CGFloat = 0.0
if topOfLine < contentOffsetTop + self.textContainerInset.top {
overflow = topOfLine - contentOffsetTop - self.textContainerInset.top
} else if bottomOfLine > bottomOfVisibleTextArea - self.textContainerInset.bottom {
overflow = (bottomOfLine - bottomOfVisibleTextArea) + self.textContainerInset.bottom
}
self.contentOffset.y += overflow
}
}