我在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
}
}
不要強行解開您自選的,你不會有這樣的問題。 –
這是很多代碼。我無法弄清楚問題是什麼或者它如何與所有代碼相關。你能把它縮小到幾行,清楚而準確地表明問題是什麼以及它發生在哪裏? – matt
我剛剛在頂部添加了一個代碼塊,其中顯示createMessageBE爲零的位置。 – jonthornham