XMPP buddyWentOnline 发现 nil

XMPP buddyWentOnline found nil

我正在尝试编写 XMPP 客户端程序。与我的服务器的连接正常,但是当我名册中的某人在线时,应用程序崩溃,因为 "buddyWentOnline" 意外发现 nil?

func xmppStream(sender: XMPPStream!, didReceivePresence presence: XMPPPresence!) {
    let presenceType = presence.type()
    let myUsername = sender.myJID.user
    let presenceFromUser = presence.from().user

    if presenceFromUser != myUsername {
        print("Did receive presence from \(presenceFromUser)")
        if presenceType == "available" {
            delegate.buddyWentOnline("\(presenceFromUser)")
        } else if presenceType == "unavailable" {
            delegate.buddyWentOffline("\(presenceFromUser)")
        }
    }
}

我发现了我的错误。 我没有为初始 viewController 设置委托:

class ViewController: UIViewController, ChatDelegate {

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    appDelegate.delegate = self
}