0

I having an issues on saving a set of CLLocations object in Swift. I keep getting an errors of

[Location setLongitude:]: unrecognized selector sent to instance 

My Location class had many-to-one relationship with my trip class. I try to save a set of coordinates when saving my trip object as well. Here is my Location Class, which refer on the class suggested on a thread here.

    @objc(Location)
class Location: NSManagedObject{
    @NSManaged var latitude: NSNumber!
    @NSManaged var longitude: NSNumber!
    @NSManaged var altitude: NSNumber!
    @NSManaged var timestamp: NSDate!
    @NSManaged var horizontalAccuracy: NSNumber
    @NSManaged var verticalAccuracy: NSNumber
    @NSManaged var speed: NSNumber
    @NSManaged var course: NSNumber
    @NSManaged var trip: Trip

    func initFromLocation(loc: CLLocation, currentTrip: Trip){
        self.latitude           = loc.coordinate.latitude
        self.longitude          = loc.coordinate.longitude
        self.altitude           = loc.altitude
        self.timestamp          = loc.timestamp

        self.horizontalAccuracy = loc.horizontalAccuracy > 0.0 ? loc.horizontalAccuracy : 0.0
        self.verticalAccuracy   = loc.verticalAccuracy > 0.0 ? loc.verticalAccuracy : 0.0
        self.speed              = loc.speed > 0.0 ? loc.speed : 0.0
        self.course             = loc.course > 0.0 ? loc.course : 0.0
        self.trip               = currentTrip
    }

    func location() -> CLLocation{
        return CLLocation(
            coordinate: CLLocationCoordinate2D(latitude: self.latitude.doubleValue, longitude: self.longitude.doubleValue),
            altitude: self.altitude.doubleValue,
            horizontalAccuracy: self.horizontalAccuracy.doubleValue,
            verticalAccuracy: self.verticalAccuracy.doubleValue,
            course: self.course.doubleValue,
            speed: self.speed.doubleValue,
            timestamp: self.timestamp
        )

    }

}

Any ideas on why I unable to set the longitude in my class or what going on around?

Community
  • 1
  • 1
Rayson
  • 1
  • 1
  • 1
    Can you please show the code where you use your `Location` class and where error occurs? – Max Komarychev Mar 07 '15 at 19:30
  • self.longitude = loc.coordinate.longitude This line triggers the errors. setLongitude issues – Rayson Mar 08 '15 at 04:28
  • How do you instantiate object or your `Location` class? Please show code that leads to invocation of `initFromLocation` – Max Komarychev Mar 08 '15 at 08:36
  • `let lm = NSEntityDescription.insertNewObjectForEntityForName(LocationEntityName, inManagedObjectContext: contxt) as Location var locationSavingError: NSError? for location in tripCoordinates{ lm.initFromLocation(location, currentTrip: tm) if contxt.save(&locationSavingError){ println("Successfully added this location \(location)") } }` ** tm is my Trip Model Object. **tripCoordinates is an array of CLLocation of the trip Hope this is clear enough. – Rayson Mar 08 '15 at 14:50

0 Answers0