Well, I had trouble to post. I'll try again.
1. I think you missed some which is easy because of the way I posted. I probably missed the end of class.
2. The const I left out are the URL for the JSON-call. I try to leave the in using your proposed method. I leave some out that I only used for testing.
3. Yes, I'm sure I get there. In updateUIWithWeatherData() print out the value in a label, and that works.
4. I think I had that but as I said easy to miss. Placed the two functions needed for TableView last in the file.
Thank you for showing a way to easier show the code.
<
//
// ViewController.swift
// WeatherApp
//
// Created by Angela Yu on 23/08/2015.
// Copyright (c) 2015 London App Brewery. All rights reserved.
//
import UIKit
import CoreLocation
import Alamofire
import SwiftyJSON
class WeatherViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource {
//Constants
let APP_ID_TRAIN2 = "9ad33f89-ad4d-4705-a7e9-b48a654064c6"
let APP_ID_SEARCH_STATION = "332b6bdc-5bb8-4ff3-8e71-870e78bb465c"
let WEATHER_URL = "h t t p: / / a p i.openweathermap.org/data/2.5/weather"
let TRAIN_URL = "h t t ps : / / a p i .resrobot.se/v2/trip?key=332b6bdc-5bb8-4ff3-8e71-870e78bb465c&originId=740098548&destId=740000006&format=json&products=16" //Sök mellan två platser
let APP_ID = "9b1864136739f58051bf9949784b3497"
//TODO: Declare instance variables here
let locationManager = CLLocationManager()
let weatherDataModel = WeatherDataModel()
let trainDataModel = TrainDataModel() //Object
//Pre-linked IBOutlets
@IBOutlet weak var weatherIcon: UIImageView!
@IBOutlet weak var cityLabel: UILabel!
@IBOutlet weak var temperatureLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//TODO:Set up the location manager here.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
//MARK: - Networking
/***************************************************************/
//Write the getWeatherData method here:
func getWeatherData(url: String, parameters: [String : String]) {
Alamofire.request(url, method: .get, parameters: parameters).responseJSON {
response in
if response.result.isSuccess {
print("Success! Got the weather data")
let weatherJSON : JSON = JSON(response.result.value!)
self.updateWeatherData(json: weatherJSON)
print(weatherJSON)
}
else {
print("Error \(String(describing: response.result.error))")
self.cityLabel.text = "Connection problem"
}
}
}
func getTrainData(url: String) {
Alamofire.request(url).responseJSON {
response in
if response.result.isSuccess {
//print("Success! Got the train data")
let trainJSON : JSON = JSON(response.result.value!)
self.updateTrainData(json: trainJSON)
//print(trainJSON)
}
else {
print("Error \(String(describing: response.result.error))")
self.cityLabel.text = "Connection problem"
}
}
}
//MARK: - JSON Parsing
/***************************************************************/
//Write the updateWeatherData method here:
func updateWeatherData(json : JSON) {
//let tempResult = json["main"]["temp"]
//print("Test", tempResult)
if let tempResult = json["main"]["temp"].double {
weatherDataModel.temperature = Int(tempResult - 273.15)
weatherDataModel.city = json["name"].stringValue
weatherDataModel.condition = json["weather"][0]["id"].intValue
weatherDataModel.weatherIconName = weatherDataModel.updateWeatherIcon(condition: weatherDataModel.condition)
updateUIWithWeatherData()
}
else {
cityLabel.text = "Väderdata kunde inte visas"
}
}
//Write the updateWeatherData method here:
func updateTrainData(json : JSON) {
//let tempResult = json["StopLocation"][2]["name"]
//let tempResult = json["Trip"][0]["LegList"]["Leg"][0]["Stops"]["Stop"][1]["name"].string
//print("Train", tempResult)
if let tempResult = json["Trip"][0]["LegList"]["Leg"][0]["Stops"]["Stop"][1]["name"].string {
trainDataModel.station = tempResult
trainDataModel.arrivalTime = json["Trip"][0]["LegList"]["Leg"][0]["Stops"]["Stop"][1]["arrTime"].stringValue
print(trainDataModel.arrivalTime)
updateUIWithTrainData()
}
else {
cityLabel.text = "Tågdata kunde inte visas"
}
}
//MARK: - UI Updates
/***************************************************************/
//Write the updateUIWithWeatherData method here:
func updateUIWithWeatherData() {
cityLabel.text = weatherDataModel.city
temperatureLabel.text = String(weatherDataModel.temperature)
weatherIcon.image = UIImage(named: weatherDataModel.weatherIconName)
}
func updateUIWithTrainData() {
//cityLabel.text = json["Trip"][0]["LegList"]["Leg"][0]["Stops"]["Stop"][1]["name"].stringValue
//temperatureLabel.text = String(weatherDataModel.temperature)
//weatherIcon.image = UIImage(named: weatherDataModel.weatherIconName)
//cityLabel.text = trainDataModel.station
//temperatureLabel.text = trainDataModel.arrivalTime
}
//MARK: - Location Manager Delegate Methods
/***************************************************************/
//Write the didUpdateLocations method here:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1] //tar sista vördet
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
locationManager.delegate = nil
print("longitude = \(location.coordinate.longitude), latitude = \(location.coordinate.latitude)")
let latitude = String(location.coordinate.latitude)
let longitude = String(location.coordinate.longitude)
let params : [String : String] = ["lat" : latitude, "lon" : longitude, "appid" : APP_ID]
getWeatherData(url : WEATHER_URL, parameters: params)
getTrainData(url: TRAIN_URL)
// cityLabel.text = "Hittade plats!"
}
}
//Write the didFailWithError method here:
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
cityLabel.text = "Hittar ej plats"
}
//MARK: - Change City Delegate methods
/***************************************************************/
//Write the userEnteredANewCityName Delegate method here:
//Write the PrepareForSegue Method here
//MARK: - TableView methods
/***************************************************************/
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = weatherDataModel.city
//cell.textLabel?.text = "test"
return cell
}
}
>