Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
David Bauer
3,863 PointsHow do i access a var or let from a fun in an other Swift File/Class
Hi, I am update the Stormy App to use the GPS Location. Therefore I created another Swift File where all the configurations and functions are managed. It works and I can print out the GPS information (longitude, latitude etc.). Like
import CoreLocation
class CoreLocationController : NSObject, CLLocationManagerDelegate {
...
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject] ) {
let location = locations.last as? CLLocation
print("didUpdateLocations: \(location!.coordinate.latitude), \(location!.coordinate.longitude)")
})
So my question is, how I can pass the long/lat information to another ViewController where I create the Network request to forecast.io
class WeeklyTableViewController: UITableViewController {
...
func retrieveWeatherForecast() {
let forecastService = ForecastService(APIKey: forecastAPIKey)
forecastService.getForecast(coordinate.lat, long: coordinate.long) {
})
I just can't make it happen - it drives me nuts.
1 Answer
Enrique MunguÃa
14,311 PointsYou can pass that information in several ways: The easiest is to declare global variables (i.e. variable outside any class or struct that can be accessed everywhere) for latitude and longitude and read them from your ViewController, I do not recommend this though because it is not very Object Oriented. Another way is to implement the CLLocationManagerDelegate protocol in the ViewController so it has access to the CLLocation object inside didUpdateLocations, something like this:
class WeeklyTableViewController: UITableViewController, CLLocationManagerDelegate {
...
func retrieveWeatherForecast() {
let forecastService = ForecastService(APIKey: forecastAPIKey)
forecastService.getForecast(coordinate.lat, long: coordinate.long) {
})
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject] ) {
let location = locations.last as? CLLocation
print("didUpdateLocations: \(location!.coordinate.latitude), \(location!.coordinate.longitude)")
})
}
If you want to keep both classes you can expose two properties for the data in CoreLocationController and add a property of type CoreLocationController to the ViewController.
class CoreLocationController : NSObject, CLLocationManagerDelegate {
var longitude: Float
var latitude: Float
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [AnyObject] ) {
let location = locations.last as? CLLocation
print("didUpdateLocations: \(location!.coordinate.latitude), \(location!.coordinate.longitude)")
latitude = location!.coordinate.latitude
longitude = location!.coordinate.longitude
}
class WeeklyTableViewController: UITableViewController{
var coreLocation: CoreLocationController = CoreLocationController()
func retrieveWeatherForecast() {
let forecastService = ForecastService(APIKey: forecastAPIKey)
let latitude = coreLocation.latitude
let longitude = coreLocation.longitude
forecastService.getForecast(coordinate.lat, long: coordinate.long) {
})
}
I'd recommend you the second option because is the more common way to do it.
David Bauer
3,863 PointsDavid Bauer
3,863 PointsThank you very much!