【问题标题】:Error: reason: '(setValue:) Cannot store object of type CLLocation at locations.0错误:原因:'(setValue:) 无法在位置存储 CLLocation 类型的对象。0
【发布时间】:2017-03-01 10:52:57
【问题描述】:

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

原因:'(setValue:) 无法将 CLLocation 类型的对象存储在位置 0。只能存储 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
}

【问题讨论】:

  • 显示。您的。代码。
  • 我添加了一些代码
  • 您需要以NSNumberNSStringNSDictionaryNSArray的格式存储它。保存在[{"longitude":xxxx, "latitude":xxxx}]

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

您的错误信息很清楚:

原因:'(setValue:) 无法将 CLLocation 类型的对象存储在位置 0。只能存储 NSNumber、NSString、NSDictionary 和 NSArray 类型的对象。

setValue: 上的 docs 说同样的话:

可以设置的数据类型有:

NSString – @“Hello World”

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

NSDictionary – @{@“key”:@“value”,@“nested”: @{@“另一个”:@“值”} }

NSArray

CLLocation 不是这些。您不能使用 setValue 存储 CLLocation。

通常的做法是将每个 CLLocation 拆分为其纬度和经度,它们是数字(注意 NSNumber 出现在列表中),并将整个事物表示为字典(注意 NSDictionary 出现在列表中)。所以,给定一个位置theLocation,你会说:

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

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

【讨论】:

  • 我理解“您不能使用 setValue 存储 CLLocation”。有什么解决办法吗?
  • 阅读编辑后的答案......谁反对这只是一个白痴,忽略他们。
猜你喜欢
  • 1970-01-01
  • 2017-02-17
  • 2017-01-21
  • 1970-01-01
  • 2022-07-28
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多