Hi everyone,
I’m having an issue that I have spent 2 days trying to figure out. I have a firebase database set up and one of my “tables” is called ‘AvailableTournaments’. In my viewcontroller I’ve added a pickerview to scroll through the available records in the database. I’m getting an error pickerview:titleForRow method where it doesn’t like me returning an array and get the error ‘Cannot convert return expression of type ‘AvailableTournaments’ to return type ‘String?’’ but I can’t figure it out. Any help would be great. I’ve included my view controller as well as the struct for available tournaments and have uploaded the project named ‘Tournament Fishing’ here: https://app.box.com/s/kp9xfrc8qrdpajmwvl38sl5kf5ods28x
//
// AvailableTournaments.swift
// TournamentFishing
//
// Created by Mike Nelson on 2/27/20.
// Copyright © 2020 CIC Solutions. All rights reserved.
//
import Foundation
struct AvailableTournaments {
var tournamentId:String?
var tournamentName:String?
var d1StartDate:String?
}
//
// RegistrationViewController.swift
// TournamentFishing
//
// Created by Mike Nelson on 2/18/20.
// Copyright © 2020 CIC Solutions. All rights reserved.
//
import UIKit
import FirebaseAuth
import FirebaseUI
import FirebaseDatabase
class RegistrationViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var tournamentPicker: UIPickerView!
var tournamentData = [AvailableTournaments]()
var dbRef:DatabaseReference?
override func viewDidLoad() {
super.viewDidLoad()
// Connect data
self.tournamentPicker.delegate = self
self.tournamentPicker.dataSource = self
dbRef = Database.database().reference()
}
override func viewDidAppear(_ animated: Bool) {
readDataOnce()
}
func readDataOnce() {
/* // Create a firebase auth ui object
let authUI = FUIAuth.defaultAuthUI()
// Check that it isn't nil
guard authUI != nil else {
return
}
*/
// Get the user
// let user = Auth.auth().currentUser
dbRef?.child("AvailableTournaments").observeSingleEvent(of: .value, with: { (snapshot:DataSnapshot) in
let snapshots = snapshot.children.allObjects as! [DataSnapshot]
for snap in snapshots {
let tournamentDict = snap.value as! [String:Any]
let tournamentID = snap.key
let tournamentName = tournamentDict["tournamentName"] as! String
let startDate = tournamentDict["d1StartDate"] as! String
let t = AvailableTournaments(tournamentId: tournamentID, tournamentName: tournamentName, d1StartDate: startDate)
self.tournamentData.append(t)
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return tournamentData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return tournamentData[row] **<<< GET THE ERROR HERE**
}
@IBAction func backTapped(_ sender: UIButton) {
let tabBarVC = self.storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.mainTabBarController)
self.view.window?.rootViewController = tabBarVC
self.view.window?.makeKeyAndVisible()
}
@IBAction func continueTapped(_ sender: UIButton) {
}
}