2016-08-27 47 views
-1

我在Swift中定期使用代表團,並已在許多項目中成功實施它。我目前有一個我不明白的問題。在我的CreateMessageBEDelegate委託觸發回調方法之後,CreateMessageBE對象在MessageViewController中爲零。在回調之前在CreateMessageBE中測試消息對象時,我可以看到它不是零。Swift代表團問題

下面的信息是對代碼的簡單介紹。任何想法爲什麼這不起作用?

已更新 具體問題 - createMessageBE爲零,請參見下面的內容。

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類

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

這裏是MessageBE類

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

不要強行解開您自選的,你不會有這樣的問題。 –

+0

這是很多代碼。我無法弄清楚問題是什麼或者它如何與所有代碼相關。你能把它縮小到幾行,清楚而準確地表明問題是什麼以及它發生在哪裏? – matt

+0

我剛剛在頂部添加了一個代碼塊,其中顯示createMessageBE爲零的位置。 – jonthornham

回答

0

我想通了,這個問題我的代碼。問題是我在self.createMessageBE的實例化完成之前試圖在委託調用中訪問self.createMessageBE。我的方法的順序和位置是錯誤的。下面我顯示最初的代碼,然後顯示新的更新代碼。

你可以看到調用returnMessageBEWithMessageSenderAndMessageReceiver()的位置是什麼使得區別。在正確的位置調用此函數後,self.createMessageBE不是零,因爲它已被實例化。我希望這可以幫助別人。

MessageViewController類

原始代碼

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類

原始代碼

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