【发布时间】:2018-07-16 22:08:43
【问题描述】:
我有从firstVC 传输到secondVC 的数据。
在firstVC 中,用户可以选择两个或一个字符串并将选定的字符串传输到secondVC。稍后在secondVC 中,用户可以将此字符串保存在 Firestore 中的文档中。
我的代码处理array of strings,但如果用户只选择一个字符串,我的代码必须只保存一个字符串。
如果第二个字符串为空,我怎么不能在 Firestore 中保存第二个字符串?
我的代码。
第二个VC:
var stringArray: [String] = []
@IBAction func confirmAndPaymentButtonTapped(_ sender: Any) {
let bookingData: [String: Any] = ["name": name, "surname": surname]
let bookingRef = Firestore.firestore().collection("Firestore")
if stringArray[0].isEmpty {
print("error in firstString")
} else {
bookingRef.document((confirmHallBooking?.id)!).
collection("BookingDate").document(selectedDateTextField.text!).
collection("BookingTime").document(stringArray[0]).setData(bookingData)
}
if stringArray[1].isEmpty {
print("error in secondString")
} else {
bookingRef.document((confirmHallBooking?.id)!).
collection("BookingDate").document(selectedDateTextField.text!).
collection("BookingTime").document(stringArray[1]).setData(bookingData)
}
}
现在,如果 stringArray[0] 或 stringArray[1] 为空,我会收到错误消息,我不明白如何无法保存空字符串。
【问题讨论】:
-
除了字符串为空时使用的打印语句之外,您还遇到什么错误?
-
@rmaddy
Thread 1: Fatal error: Index out of range在控制台中我看不到 printerror in secondString -
指出导致该错误的确切代码行。
-
@rmaddy 在这条线上
if secondString.isEmpty {错误Thread 1: Fatal error: Index out of range -
@rmaddy 我稍微更改了有问题的代码,请看。在这一行我得到一个错误
if stringArray[1].isEmpty错误Thread 1: Fatal error: Index out of range
标签: ios swift database google-cloud-firestore