Posts

Post not yet marked as solved
3 Replies
Well, I could show code but the problem is not the values.It's when I change the position of the UITable or labels and so on.
Post marked as solved
5 Replies
I don't want to start a new post unnecessarily, so I try here first.As you saw I have been using SwiftyJSON because they did it in the course. Also, it should be easier, but I'm not sure.Trying to follow your example, I tried this:for item in json["Trip"][0]["LegList"]["Leg"][0]["Product"].arrayValue { print(item["operator"].stringValue) }Here I try to print the value, but what I really want to do is sort out some values.So if operator is "SJ" then it should not show, but "Öresundståg" should.I don't get any errors, but nothing shows.I've read something about encodeble that's is built in. So even though you aren't use to SwiftyJSON, do you have any suggestion?It doesn't have to be SwiftyJSON.Or is there a better way to sort out values in JSON?
Post marked as solved
2 Replies
Ok, I'll check it out, thanks!
Post marked as solved
5 Replies
That seemed to work, thanks!
Post marked as solved
5 Replies
No, I don’t have people or firstname. That’s in the example JSON-string. So by translate I mean how to go through my JSON response in similar way.So what I want to do is parse through my response. It’s train-travels.I would like to say pick out say three trips with start and endpoint. Exactly how to do this I can figure out but would like to see how to pick out something like every station in a trip.
Post marked as solved
32 Replies
It worked!Thank you both!I'm sorry that I couldn't mark both as correct answers.Clearly I have to learn more about the basics.One question: Could I have made this connection completely in code?Question two: I want to display two values in one cell. The staition from the API on the left and the time on the right.Without explaining everything, could you give me a hint to the right solution?
Post marked as solved
32 Replies
Not sure. I connected the outlet to source and delegate.If this was working I'm not sure.How can I check this?
Post marked as solved
32 Replies
Yes, it is at the linetableView.delegate = self That the error fist occurs. But I can show a value set in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { But not the value from the model.So I can docell.textLabel?.text = "test" But notcell.textLabel?.text = self.weatherDataModel.city But I know that the value is set in functionupdateWeatherData
Post marked as solved
32 Replies
So this is what I get after trying the suggestions from OOPer.Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional valueI think I somewhat understand Optionals but not entirely.It does suggest I guess that the object in tableView is empty. I don't understand why since.it's set in updateWeatherData.I have already connected to delegate and source in storyboard if that matters.
Post marked as solved
32 Replies
Thanks! I will try your suggestions when I am at my computer again. I am kind of a beginner and haven’t fully graspt everything. My approach is to make my own app while learning.
Post marked as solved
32 Replies
Hi! I really appreciate your help.I am trying to post in one file but keep getting the message "being moderated"Thinking that the links are the problem I removed som of the link for weather_URL and TRAIN_URL.Weather_URL are from the course I'm following and TRAIN_URL are my own testing.If the file is approved I have tried to anwer your question.The removal of the links didn't seem to help, and you need the links anyway. Adding space didn't seem to help-If you doesn't have a solution I just have to wait until it is approved.
Post marked as solved
32 Replies
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 UIKitimport CoreLocationimport Alamofireimport SwiftyJSONclass 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 }}>
Post marked as solved
32 Replies
//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 }}
Post marked as solved
32 Replies
//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 }
Post marked as solved
32 Replies
//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" } }