简体   繁体   中英

Ambiguous use of 'subscript' and Cannot call value of non-function type 'AnyObject' errors retrieving data from Firebase in SWIFT 4.1

I'm changing the way I'm posting and retrieving firebase CLLocationCoordinated2D, from one post per value to one post with all values, so I found this post and I would like to implement it in my own code. Retrieving data from Firebase and storing as annotations on a map . Im having the errors mentioned in the title on the constants date, time, latitude, longitude and desc. I'm still learning Firebase so any explanation will be very helpful. Tis is the function where I get the errors.

   func displayAnnotations() {

        let ref = Database.database().reference()
        ref.child("Sightings").observe(.childAdded, with: { (snapshot) in

            let date = (snapshot.value as AnyObject?)!("Date") as! String?
            let time = (snapshot.value as AnyObject)!("Time") as! String?
            let latitude = (snapshot.value as AnyObject)!("Latitude") as! String?
            let longitude = (snapshot.value as AnyObject?)!("Longitude") as! String?
            let desc = (snapshot.value as AnyObject?)!("Description") as! String?

            let annotation = MKPointAnnotation()

            annotation.coordinate = CLLocationCoordinate2D(latitude: (Double(latitude!))!, longitude: (Double(longitude!))!)
            annotation.title = date
            annotation.subtitle = time
            self.mapView.addAnnotation(annotation)

        })}

and this is the posting function:

func post() {

        let date = dateLabel.text
        let time = timeLabel.text
        let latitude = latitudeLabel.text
        let longitude = longitudeLabel.text
        let sightingDescription = descriptionLabel.text

        let post: [String:String] = ["Date" : date as AnyObject,
                                          "Time" : time as AnyObject,
                                          "Latitude" : latitude as AnyObject,
                                          "Longitude" : longitude as AnyObject,
                                          "Description" : sightingDescription as AnyObject]

        var ref: DatabaseReference!
        ref = Database.database().reference()
        ref.child("Sightings").childByAutoId().setValue(post)

    }

Is it just because it's written for a version previous to swift 4.1?

You changed [] to ()

let dic = snapshot.value as! [String:String]
let date = dic["Date"] 
let time = dic["Time"]   
let latitude = dic["Latitude"] 
let longitude = dic["Longitude"]  
let desc = dic["Description"]  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM