我有一个包含textField和button的VC。我有一个简单的任务是将textField文本数据传递给我的模型实体。但是,虽然我想将textField数据分配给实体数据,但我的数据仍为零。 我无法理解是什么导致了这样的问题
class InputTextViewController: UIViewController {
@IBOutlet weak var messageTextField: UITextField!
//I receive user from parent VC by segue
var userSendMessageTo: User!
var tableView: UITableView?
var message: Message { return Message(userSendMessageTo) }
// initialising object of my entity class
override func viewDidLoad() {
super.viewDidLoad()
print(userSendMessageTo.name)
//name is right
}
func createMessage() {
message.messageText = messageTextField.text
message.messageTime = Date()
print(message.messageText) //nil
print(message.messageTime) //nil
}
@IBAction func sendMessge(_ sender: Any) {
createMessage()
userSendMessageTo.mesaageHistory.append(message)
print(userSendMessageTo.mesaageHistory[0].messageText) //nil
}
我的模特
class Message {
var messageText: String?
var messageTime: Date?
var messageInage: UIImage?
var user: User
init(_ user: User) {
self.user = user
}
}
class User {
let name: String
var mesaageHistory = [Message]()
init(name: String) {
self.name = name
}
}
答案 0 :(得分:2)
构造var message: Message { return Message(userSendMessageTo) }
每次调用时都会返回一个新的Message对象。
因此,您正在创建一个消息对象,为其分配文本,然后将其丢弃。然后创建第二个消息对象,为其指定日期并将其丢弃。然后创建第三个消息对象并检查其文本(为零),然后创建第四个消息对象并检查其日期,也是零。
请改为尝试:
class InputTextViewController: UIViewController {
@IBOutlet weak var messageTextField: UITextField!
var userSendMessageTo: User!
var tableView: UITableView?
var message: Message?
func createMessage() {
message = Message()
message!.messageText = messageTextField.text
message!.messageTime = Date()
print(message!.messageText) //won't be nil
print(message!.messageTime) //won't be nil
}
}
答案 1 :(得分:2)
您已将message
声明为计算变量。这意味着每次引用message
时,您实际上都在执行以下代码块:
Message(userSendMessageTo)
换句话说,对message
的每次引用都会创建Message
的新实例。
message.messageText = messageTextField.text
- 创建Message
message.messageTime = Date()
- 创建Message
print(message.messageTime)
- 创建一个新的Message实例,其messageTime
属性为nil 此处无需使用房产;您的createMessage
函数应该返回新的Message
:
func createMessage(withText text: text) -> Message {
let message = Message(userSendMessageTo)
message.messageText = text
message.messageTime = Date()
return message
}
@IBAction func sendMessge(_ sender: Any) {
let message = self.createMessage(withText: messageTextField.text)
userSendMessageTo.mesaageHistory.append(message)
print(userSendMessageTo.mesaageHistory[0].messageText)
}
说实话,如果你为Message
创建一个合适的初始化程序,你可以完全摆脱createMessage
函数:
class Message {
var messageText: String
var messageTime: Date
var messageInage: UIImage?
var user: User
class Message {
var messageText: String
var messageTime: Date
var messageInage: UIImage?
var user: User
init(_ user: User, text: String = "", date: Date = Date()) {
self.user = user
self.messageTime = date
self.messageText = text
}
}
然后你的行动方法就变成了:
@IBAction func sendMessge(_ sender: Any) {
let message = Message(userSendMessageTo, text: messageTextField.text, date:Date())
userSendMessageTo.mesaageHistory.append(message)
print(userSendMessageTo.mesaageHistory[0].messageText)
}