Ios ViewController实例出现错误“展开可选值时发现为零”

Ios ViewController实例出现错误“展开可选值时发现为零”,ios,uitableview,uiviewcontroller,swift2,Ios,Uitableview,Uiviewcontroller,Swift2,我正在尝试使用Swift 2将MyChatController加载到另一个控制器中。此代码在Swift 1.2中运行良好,但在更新到Xcode 7后,我的应用程序崩溃,出现以下错误: 致命错误:在展开可选值时意外发现nil 有人知道如何在Swift 2中解决这个问题吗 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let chatCo

我正在尝试使用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)

  }

我已经看过你的密码了。 有一个错误。请完成以下步骤

转到StretchTextView中名为align的方法。 使用此方法,您将尝试打开nil self.selectedTextRange?.end!。最初选择的extrange将为零。 因此,在方法的开头,如果self.selectedTextRange为nil,则返回

函数对齐{

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
}
}


不确定,但您可以为let chatController尝试以下语法:MyChatController=MyChatController!self.navigationController!。pushViewControllerchatController,动画:False这到底是在哪一行发生的?是的,我尝试了所有可能的方法,但都不起作用。@mrunalthanki@Nishant在这一行触发错误,让chatController:MyChatController=MyChatControllerSo MyChatController返回nil,然后呢?我们可能需要了解这方面的来源。