Swift 委派问题

Swift Delegation Issue

我经常在 Swift 中使用委派,并已在多个项目中成功实施。我目前遇到一个我不明白的问题。在我的 CreateMessageBEDelegate 委托触发回调方法后,CreateMessageBE 对象在 MessageViewController 中为 nil。在回调之前在 CreateMessageBE 中测试消息对象时,我可以看到它不是零。

以下信息是对代码的演练。知道为什么这不起作用吗?

已更新 具体问题 - createMessageBE 为 nil,请参见下文。

func createMessageBEWithMessageSenderAndMessageReceiverDidSuceed(createMessageBEInput: CreateMessageBE)
    {
        self.message = self.createMessageBE?.message  // createMessageBE is nil

        // Update Backendless
        self.updateBackendless = UpdateBackendless(messageBEWithMessageSenderAndMessageReceiver: self.message!, delegateInput: self)
    }

MessageViewController

class MessageViewController : UIViewController, FindMessageSenderDelegate, FindMessageReceiverDelegate, CreateMessageBEDelegate, UpdateBackendlessDelegate
{
    // Backendless Property
    let backendless = Backendless.sharedInstance()

    // Message Properties
    var messageSender : BackendlessUser?
    var MessageReceiver : BackendlessUser?
    var messageText : String?
    var message : MessageBE?

    var findMessageSender : FindMessageSender?
    var findMessageReceiver : FindMessageReceiver?
    var createMessageBE : CreateMessageBE?
    var updateBackendless : UpdateBackendless?

func findMessageReceiverDidFindUsernameMessageReceiver(findMessageReceiverInput : FindMessageReceiver)
    {
        // Set the Message User Message Receiver
        self.messageReceiver = self.findMessageReceiver!.messageReceiver

        self.createMessageBE = CreateMessageBE(guestMessageUserInput: self.guestMessageUser!, messageReceiverInput: self.messageReceiver!, messageInput: self.message!, delegateInput: self)
        //print(self.createMessageBE)  If I call this here self.createMessage is not nil in the delegate call back.  If I do not call it, it is nil.
    }

func createMessageBEWithMessageSenderAndMessageReceiverDidSuceed(createMessageBEInput: CreateMessageBE)
    {
        // Set the MessageBE Property
        self.message = self.createMessageBE?.message

        // Update Backendless
        self.updateBackendless = UpdateBackendless(messageBEWithMessageSenderAndMessageReceiver: self.message!, delegateInput: self)
    }

这里是 CreateMessageBE class

import Foundation
import CoreData

protocol CreateMessageBEDelegate
{
    func createMessageBEWithMessageSenderAndMessageReceiverDidSuceed(createMessageBEInput : CreateMessageBE) -> ()
}

class CreateMessageBE : NSObject
{
    // Properties
    var message : MessageBE?

    // Backendless Property
    let backendless = Backendless.sharedInstance()

    // Delegate Property
    var delegate : CreateMessageBEDelegate?

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    // Custom Init Method
    init(messageSenderInput: BackendlessUser, messageReceiverInput: BackendlessUser, messageTextInput: String, delegateInput: CreateMessageBEDelegate)
    {
        super.init()

        // Create MessageBE for Message User Sender and Message User Receiver
        self.message = MessageBE(messageSenderInput: messageSenderInput, messageReceiverInput: messageReceiverInput, messageTextInput: messageTextInput)
        self.delegate = delegateInput

        self.returnMessageBEWithMessageSenderAndMessageReceiver()
    }
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    func returnMessageBEWithMessageSenderAndMessageReceiver()
    {
        // Return the created MessageBE to MessageViewController
        if self.message != nil
        {
            print("In the createMessageBEDelegate and printing self.message \(self.message)")
            self.delegate?.createMessageBEWithMessageSenderAndMessageReceiverDidSuceed(self)
        }
        else
        {
            // Report error.
        }
    }
}

这是消息BE class

import Foundation
import UIKit

class MessageBE: NSObject
{
    // This is a custom class used to access Message Objects that are stored Backendless.
    var objectId : String?
    var created : NSDate?
    var updated : NSDate?
    var messageSender : BackendlessUser?
    var messageReceiver : BackendlessUser?
    var messageText : String?
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    init(messageSenderInput: BackendlessUser, messageReceiverInput: BackendlessUser, messageTextInput: Double)
    {
        self.messageSender = messageSenderInput
        self.messageReceiver = messageReceiverInput
        self.messageText = messageTextInput
    }
}

我发现了我的代码的问题。问题是在 self.createMessageBE 的实例化完成之前,我试图在委托回调中访问 self.createMessageBE。我的方法的顺序和位置是错误的。下面我展示了初始代码,然后是新的更新代码。

您可以看到调用 returnMessageBEWithMessageSenderAndMessageReceiver() 的位置是不同之处。在正确的位置进行此调用后,self.createMessageBE 不是 nil,因为它已被实例化。我希望这可以帮助别人。

MessageViewControllerClass

原代码

    func findMessageReceiverDidFindUsernameMessageReceiver(findMessageReceiverInput : FindMessageReceiver)
        {
            // Set the Message User Message Receiver
            self.messageReceiver = self.findMessageReceiver!.messageReceiver

            // Create createMessageBE
            self.createMessageBE = CreateMessageBE(guestMessageUserInput: self.guestMessageUser!, messageReceiverInput: self.messageReceiver!, messageInput: self.message!, delegateInput: self)     
        }

新代码

func findMessageReceiverDidFindUsernameMessageReceiver(findMessageReceiverInput : FindMessageReceiver)
    {
        // Set the Message User Message Receiver
        self.messageReceiver = self.findMessageReceiver!.messageReceiver

        self.createMessageBE = CreateMessageBE(guestMessageUserInput: self.guestMessageUser!, messageReceiverInput: self.messageReceiver!, messageInput: self.message!, delegateInput: self)
        self.createMessageBE?.returnMessageBEWithMessageSenderAndMessageReceiver()
    }

CreateMessageBE Class

原代码

// Custom Init Method
    init(messageSenderInput: BackendlessUser, messageReceiverInput: BackendlessUser, messageTextInput: String, delegateInput: CreateMessageBEDelegate)
    {
        super.init()

        // Create MessageBE for Message User Sender and Message User Receiver
        self.message = MessageBE(messageSenderInput: messageSenderInput, messageReceiverInput: messageReceiverInput, messageTextInput: messageTextInput)
        self.delegate = delegateInput

        self.returnMessageBEWithMessageSenderAndMessageReceiver()
    }

新代码

// Custom Init Method
    init(messageSenderInput: BackendlessUser, messageReceiverInput: BackendlessUser, messageTextInput: String, delegateInput: CreateMessageBEDelegate)
    {
        super.init()

        // Create MessageBE for Message User Sender and Message User Receiver
        self.message = MessageBE(messageSenderInput: messageSenderInput, messageReceiverInput: messageReceiverInput, messageTextInput: messageTextInput)
        self.delegate = delegateInput
    }