Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Cannot convert return expression of type 'Sweet' to return type 'String?'

hi,

I am trying to use my array from Firebase in a pickerView but it's not having it as the array aparantly is not a string?!
import UIKit
import FirebaseDatabase
import FirebaseAuth

class categoriesViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    @available(iOS 2.0, *)
    public func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    @IBOutlet var label: UILabel!
    @IBOutlet weak var picker: UIPickerView!
    var dbRef:FIRDatabaseReference!
    var sweets = [Sweet]()
    var food = ["hello","world","this is john"]
    override func viewDidLoad() {
        super.viewDidLoad()

        dbRef = FIRDatabase.database().reference().child("sweet-items")
        startObservingDB()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func startObservingDB(){
        
        dbRef.observe(FIRDataEventType.value, with: { (snapshot:FIRDataSnapshot) in
            var newSweets = [Sweet]()
            
            
            for sweet in snapshot.children.allObjects{
                let sweetObject = Sweet(snapshot: sweet as! FIRDataSnapshot)
                newSweets.append(sweetObject)
            }
            self.picker.delegate = self
            self.picker.dataSource = self
            
            self.sweets = newSweets
            
        })
        
    }
    
    // The number of columns of data
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return sweets[row]
        
    }
    // The number of rows of data
   
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return sweets.count
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        label.text = sweets[row]
    }

}

if I was to use the VAR food in my pickerView functions then it works

I get the error:

Cannot convert return expression of type 'Sweet' to return type 'String?'

Replies

Sign In or Register to comment.