Search code examples
iosswiftserializationswift2

Issue passing data from API


This code is from the locations tableView view controller, where the locations are serialized from a Four Square API and I grab the data, passing it back the view controller where I create an event. The data is serialized correctly, populating the tableView and all, so I won't bother posting that for now, just the segue method to pass the data.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
    let indexPath = self.tableView.indexPathForSelectedRow
    
    let item = self.responseItems![indexPath!.row]
    
    var eventVC = CreateEventViewController()
    
    if segue.identifier == "pushBackToEventVC" {
        
        if let venue = item["venue"] as? NSDictionary {
            
            let locationString = venue["name"] as! String
            let location = venue["location"]
            
            //get lat/long strings
            
            print(location)
            
            eventVC.updateWithLocation(locationString)
            eventVC.updateWithGeoPoint(PFGeoPoint(latitude: locationLatitude, longitude: locationLongitude))
            
        }
        
        eventVC = segue.destinationViewController as! CreateEventViewController
        
        //pass location coordinates 
    }

//the rest of the segue method 

}

The update methods in the create event view controller, when I put a breakpoint on these methods I can see the data has been passed correctly, and the location (a PFGeoPoint) works okay but the locationString (a string) throws a "Bad instruction error" although it does print it correctly to the console:

func updateWithLocation(locationString: String) {
    
    print(locationString)
    self.locationLabel.text = locationString
}

func updateWithGeoPoint(venueLocation: PFGeoPoint) {
    
//        print(venueLocation)
    self.event?.location = venueLocation
    self.eventLocation = venueLocation
    print(self.eventLocation)
}

Any ideas? It's basically working but won't update the label with the string although I can see it's been passed correctly.


Solution

  • Try using this code block instead,

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "pushBackToEventVC") {
    
            let detailViewController = ((segue.destinationViewController) as! CreateEventViewController)
            let indexPath = self.tableView!.indexPathForSelectedRow!
    
            detailViewController.locationString = venue["location"]
        }
    }