错误:原因:'(setValue :)无法在locations.0存储CLLocation类型的对象

时间:2017-03-01 00:20:39

标签: ios swift firebase firebase-realtime-database

尝试将let myLocations[CLLocation]保存到Firebase,但收到错误:

  

原因:'(setValue :)无法在locations.0存储CLLocation类型的对象。只能存储NSNumber,NSString,NSDictionary和NSArray类型的对象。

有任何帮助吗?

代码:

var myLocations: [CLLocation] = []

  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        myLocations.append(locations[0] as CLLocation)

        let spanX = 0.007
        let spanY = 0.007

        var location = CLLocation()
        for L in myLocations {
            location = L
        }
        let newRegion = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
        theMap.setRegion(newRegion, animated: true)

        if (myLocations.count > 3){
            let sourceIndex = myLocations.count - 1
            let destinationIndex = myLocations.count - 4
            let c1 = myLocations[sourceIndex].coordinate
            let c2 = myLocations[destinationIndex].coordinate
            var a = [c1, c2]
            let polyline = MKPolyline(coordinates: &a, count: a.count)
            polyline.title = "polyline"
            theMap.add(polyline)
        }

        if startLocation == nil {
            startLocation = locations.first as CLLocation!
        } else {
            let lastDistance = lastLocation.distance(from: locations.last as CLLocation!) //In Meter
            distanceTraveled += lastDistance * 0.000621371192 //In Miles
            //1 Meter = 0.000621371192 Miles
            //1 Mile = 1609.344 Meters
            distanceLabel_String = String(format: "%.2f  mi", distanceTraveled)
            distanceLabel.text = distanceLabel_String
            let altitude = lastLocation.altitude // In Meters
            let altitudeInFeets = altitude / 0.3048 //In Feets
            arrayOfAltitude.append(altitudeInFeets)
            let maxAltitude = arrayOfAltitude.max()
            altitudeLabel_String = String(format: "%.2f ft", maxAltitude!)
            altitudeLabel.text = String(format: "%.2f ft", altitudeInFeets)
        }

        lastLocation = locations.last as CLLocation!

    }




func saveLocations() {

    let locations = myLocations

    let trailInfo: Dictionary<String, Any> = [ "locations" : locations,]


        let databaseRef = FIRDatabase.database().reference()
        databaseRef.child("Trails").childByAutoId().setValue(trailInfo)
    }

@IBAction func doneActivityHit(_ sender: UIButton) {
    saveLocations()
    self.view.removeFromSuperview()
    myLocations.removeAll()
    distanceTraveled = 0
}

1 个答案:

答案 0 :(得分:1)

您的错误消息很明确:

  

原因:&#39;(setValue :)无法在locations.0存储CLLocation类型的对象。只能存储NSNumber,NSString,NSDictionary和NSArray类型的对象。

setValue:上的docs说同样的话:

  

可以设置的数据类型是:

     

NSString - @“Hello World”

     

NSNumber(也包括布尔值) - @YES,   @ 43,@ 4.333

     

NSDictionary - @ {@“key”:@“value”,@“nested”:   @ {@“another”:@“value”}}

     

的NSArray

CLLocation不是其中任何一个。您无法使用setValue存储CLLocation。

通常的做法是将每个CLLocation分成纬度和经度,这些是数字(注意NSNumber出现在列表中),并将整个事物表示为字典(注意NSDictionary出现在列表中)。因此,给定位置theLocation,您会说:

let dict = ["Latitude": theLocation.coordinate.latitude, "Longitude": theLocation.coordinate.longitude]

您可以为要存储的每个位置执行此操作(并反转检索和重建位置的过程)。