我想在文本数据中加载form_Mesaj1,但是在form_Mesaj1行中遇到此错误。有什么问题吗?
导入基金会
public struct Message {
var text:String
let isIncoming:Bool
let date:Date
}
class MessageViewController: ChatVCWithTableView {
let form_Mesaj1 = String()
private let messages = [
[
Message(text:"\(form_Mesaj1)", isIncoming:true, date: Date.dateFromCustomString(dateString: "12/22/2018"))
]
答案 0 :(得分:0)
编译器不确定form_Mesaj1
将在messages
之前进行初始化。可以确定,只有之后 self
(所有存储的属性)已初始化。
因此您可以将其设为lazy
:
private lazy var messages = [
Message(text:"\(form_Mesaj1)", isIncoming: true, date: Date.dateFromCustomString(dateString: "12/22/2018"))
]
这样,编译器可以确保在使用form_Mesaj1
之前已对其进行初始化,因为您必须在现有的(“ aka”已初始化的)对象上调用messages
,对吧?!
还可以手动初始化它们:
class MessageViewController: ChatVCWithTableView {
let form_Mesaj1: String
private let messages: [Message]
init(form_Mesaj1: String) {
self.form_Mesaj1 = form_Mesaj1
messages = [
Message(text:"\(form_Mesaj1)", isIncoming: true, date: Date.dateFromCustomString(dateString: "12/22/2018"))
]
}
}