This is my first Swift/Firestore practice app and I am trying to gain access to the variables (i.e. eventLocation, eventDate) in the 'AddEventViewController' file to be used in 'EventTableViewController' file.
The variable will then be inserted at ?? to query a Firestore database
Firestore.firestore().collection("Locations").document(??).collection("Dates").document(??).collection("Events").getDocuments()
I have tried to most swift tutorials on the web but it has made me even more confused as I can't seem to figure out what I am missing or what I am doing wrong....
Would someone be kind enough to help a newbie to swift programming? Many thanks in advance!
EventTableViewController.swift
import UIKit
import FirebaseFirestore
class EventsTableViewController: UITableViewController {
var eventsArray = [Event]()
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
func loadData() {
Firestore.firestore().collection("Locations").document(??).collection("Dates").document(??).collection("Events").getDocuments() { (querySnapshot, error) in
if let error = error {
print("Error getting documents: \(error)")
} else {
self.eventsArray = querySnapshot!.documents.compactMap({Event(dictionary: $0.data())})
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return eventsArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "eventCell", for: indexPath)
let event = eventsArray[indexPath.row]
// Configure the cell...
cell.textLabel?.text = "\(event.programType)"
cell.detailTextLabel?.text = "\(event.eventLocation) \(event.eventDate)"
return cell
}
AddEventViewController.swift
import UIKit
import FirebaseFirestore
class AddEventViewController: UIViewController {
@IBOutlet weak var eventLocation: UITextField!
@IBOutlet weak var eventDate: UITextField!
@IBOutlet weak var programType: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func saveEventButtonTapped(_ sender: UIBarButtonItem) {
guard let EventLocation = eventLocation.text, !EventLocation.isEmpty else { return }
guard let EventDate = eventDate.text, !EventDate.isEmpty else { return }
guard let Program = programType.text, !Program.isEmpty else {return}
// Save profile details of person to Firestore as data fields
let eventDetail: [String: Any] = ["Event Location": EventLocation, "Event Date": EventDate, "Program": Program]
// Write data to Firestore
Firestore.firestore().collection("Locations").document(EventLocation).collection("Dates").document(EventDate).collection("Events").document(Program).setData(eventDetail)
{ error in
if let error = error {
print("ERROR: Event not saved to database. Please try again! \(error)")
} else {
print("Event has been saved to the database!")
}
self.dismiss(animated: true, completion: nil)
}
}
Event.swift
struct Event {
var programType: String
var eventLocation: String
var eventDate: String
var dictionary: [String: Any] {
return [
"programType": programType,
"eventLocation" : eventLocation,
"eventDate" : eventDate
]
}
}
extension Event : DocumentSerializable {
init?(dictionary: [String: Any]) {
guard let programType = dictionary["Program"] as? String,
let eventLocation = dictionary["Event Location"] as? String,
let eventDate = dictionary["Event Date"] as? String else { return nil}
self.init(programType: programType, eventLocation: eventLocation, eventDate: eventDate)
}
}