I get a error: "fatal error: unexpectedly found nil while unwrapping an Optional value" when I try to pass the result of the JSON into an array. I think that the problem is when I try to get the geo coordinate: lat and long into my variables.
Please can somebody help me with this issue?
The code for the passing de result of the JSON to an array is:
ViewController.swift
import UIKit
import MapKit
import Alamofire
class ViewController: UIViewController {
var artworks = [Artwork]()
#IBOutlet weak var mapView: MKMapView!
let initialLocation = CLLocation(latitude: 52.370216, longitude: 4.895168)
let regionRadius: CLLocationDistance = 6000
override func viewDidLoad() {
super.viewDidLoad()
centerMapOnLocation(initialLocation)
getData()
mapView.addAnnotations(artworks)
mapView.delegate = self
}
func getData() {
Alamofire.request(.GET , "http://kaart.amsterdam.nl/datasets/datasets-item/t/kunstwerken-in-de-openbare-ruimte/export/json").responseJSON() {
(_, _, json, _) in
println(json)
var json = JSON(json!)
if let appArray = json["features"].array {
for artObject in appArray {
let title = artObject["properties"]["titel"].string
let locationName = artObject["properties"]["titel_key"].string
let discipline = artObject["properties"]["titel_key"].string
let latitude = (artObject["geometry"]["coordinates"].string! as NSString).doubleValue
let longitude = (artObject["geometry"]["coordinates"].string! as NSString).doubleValue
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
var openArt1 = Artwork(title: title!, locationName: locationName!, discipline: discipline!, coordinate: coordinate)
self.artworks.append(openArt1)
self.mapView.addAnnotations(self.artworks)
}
}
}
}
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
the result of the JSON looks like this:
Optional({
features = (
{
geometry = {
coordinates = (
"4.9305202013246",
"52.363277804149"
);
type = Point;
};
properties = {
adres = "";
content = "";
"content_src" = "";
"date_created" = "2015-05-16 00:07:58";
"date_modified" = "<null>";
datum = "2013-10-09 17:15:00";
email = "";
"id_2" = "";
"link_href" = "";
locatie = "POINT(4.93052020132461 52.363277804148602)";
omschrijving = "";
plaats = "";
postcode = "";
published = "";
summary = "";
"summary_type" = "";
telefoonnummer = "";
titel = "101 namen - 136602";
"titel_key" = "101_namen_136602";
title = "";
type = Kunstwerk;
updated = "";
website = "http://www.amsterdam.nl/kunstencultuur/kunst-openbare/kunst-openbare/kunstwerken/amsterdam-stadsdeel-0/dapperbuurt/101-namen-136602/";
};
artObject["geometry"]["coordinates"] is an Array of Doubles, not a String.
If you println the JSON response it could be confusing as you see the Doubles wrapped into quotes (") but it is an array of Doubles nonetheless in the JSON for the "coordinates" key.
UPDATE:
I just checked and they are Doubles:
var err: NSError?
let data = NSData(contentsOfURL: NSURL(string: "http://kaart.amsterdam.nl/datasets/datasets-item/t/kunstwerken-in-de-openbare-ruimte/export/json")!)
let json = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.allZeros, error: &err) as! [String:AnyObject]
let features = json["features"] as! [[String:AnyObject]]
let object1 = features[0]
let geo = object1["geometry"]?["coordinates"] as! [Double]
let lat = geo[0] // 4.9305202013246
let lon = geo[1] // 52.363277804149
You just have to access these values with SwiftyJSON like this in your loop:
let coo = artObject["geometry"]["coordinates"].array!
let latitude = coo[0].doubleValue
let longitude = coo[1].doubleValue
Related
I do a JSON Request and get some information.
func parseJSON(poiData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(POIData.self, from: poiData)
POIManager.POIname_One = decodedData.results[0].name
POIManager.POIplaceid_One = decodedData.results[4].place_id
POIManager.POIvicinity_One = decodedData.results[4].vicinity
POIManager.POIlong_One = decodedData.results[0].geometry.location.lat
POIManager.POIlat_One = decodedData.results[0].geometry.location.lng
POIManager.POIname_Two = decodedData.results[1].name
POIManager.POIplaceid_Two = decodedData.results[1].place_id
POIManager.POIvicinity_Two = decodedData.results[1].vicinity
POIManager.POIlong_Two = decodedData.results[1].geometry.location.lat
POIManager.POIlat_Two = decodedData.results[1].geometry.location.lng
POIManager.POIname_Three = decodedData.results[2].name
POIManager.POIplaceid_Three = decodedData.results[2].place_id
POIManager.POIvicinity_Three = decodedData.results[2].vicinity
POIManager.POIlong_Three = decodedData.results[2].geometry.location.lat
POIManager.POIlat_Three = decodedData.results[2].geometry.location.lng
} catch {
print(error)
}
}
In a different Swift file it put the results from the request in a list like this:
#IBAction func kategorieEins(_ sender: UIButton) {
//Eigene Standort soll hier gezeigt werden/aktualisierter Standort
locationManager.delegate=self
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_One, longitude: POIManager.POIlat_One)
marker1.title = POIManager.POIname_One
marker1.snippet = "Marker1_0"
marker1.map = mapView
let marker2 = GMSMarker()
marker2.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_Two, longitude: POIManager.POIlat_Two)
marker2.title = POIManager.POIname_Two
marker2.snippet = "Marker2_0"
marker2.map = mapView
let marker3 = GMSMarker()
marker3.position = CLLocationCoordinate2D(latitude: POIManager.POIlong_Three, longitude: POIManager.POIlat_Three)
marker3.title = POIManager.POIname_Three
marker3.snippet = "Marker3_0"
marker3.map = mapView
}
As you can see this whole thing is not dynamic it is static. I write down how many markers i want to have created.
Is there a way to do this automatically? Especially when I dont know how much information there is in the json file and how many markers should be created.
I solved it now.
I tried:
decodedData.results.forEach {
print($0.name)
print($0.place_id)
}
I want to extract the names of these coffes this is an extract of my output because i have like 1000 names, and i want to take them all automatically :
results = (
{
geometry = {
location = {
lat = "-33.3979227";
lng = "-70.58503859999999";
};
viewport = {
northeast = {
lat = "-33.39783990000001";
lng = "-70.58502229999999";
};
southwest = {
lat = "-33.39795669999999";
lng = "-70.58507830000001";
};
};
};
id = 46354da06de96a36c5c44a5fa05a10f8f83f8edd;
name = "Hotel Bidasoa";
"opening_hours" = {
"open_now" = 1;
"weekday_text" = (
);
};
}
);
"place_id" = ChIJ4dfUCC7PYpYRRDkSNifrfBE;
rating = "4.7";
scope = GOOGLE;
types = (
cafe,
lodging,
food,
store,
"point_of_interest",
establishment
);
vicinity = "Avenida Vitacura 4873, Santiago, Santiago";
},
{
geometry = {
location = {
lat = "-33.37900460000001";
lng = "-70.55533029999999";
};
viewport = {
northeast = {
lat = "-33.37897230000002";
lng = "-70.5553148";
};
southwest = {
lat = "-33.37910149999999";
lng = "-70.55537679999999";
};
};
};
id = c451d2146b7a065fa1afd0ffa39353a4b1cae178;
name = "Ceibo Emporio Cafeter\U00eda";
"opening_hours" = {
"open_now" = 0;
"weekday_text" = (
);
};
and thi is my code, but only prints me the first name i want both because i have like 1000 names:
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print (jsonResult)
if let nombre = ((jsonResult["results"]as?NSArray)?[0]as?NSDictionary)?["name"] {
print (nombre)
}
As always:
Never use NSDictionary / NSArray in Swift when parsing JSON.
Never use mutableContainers in Swift. It's completely useless.
To get all items in the results array use a loop, for convenience and readability use a type alias:
typealias JSONDictionary = [String:Any]
if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? JSONDictionary {
print (jsonResult)
if let results = jsonResult["results"] as? [JSONDictionary] {
for result in results {
print(result["name"] as? String ?? "n/a")
if let geometry = result["geometry"] as? JSONDictionary,
let location = geometry["location"] as? JSONDictionary {
let lat = location["lat"] as? Double ?? 0.0
let lng = location["lng"] as? Double ?? 0.0
print("latitude: \(lat)")
print("longitude: \(lng)")
}
}
}
}
Im new to Swift and I'm trying to use the Google Directions API. I have modified a function to get a Polyline and put the Google directions into an array. The array of directions I declared as a property of the class I'm working in so I can display the results in a table. I have checked and the array is be properly populated with the directions inside the closure. However when I try to use it outside the closure in the CellForTableAT function it is empty. I'm probably not getting the data out of the closures correctly but I cant figure it out. I begin building my array at //MARK: Start Building Directions Array using swiftyJason
func getDirections(currentDestination: GMSMarker, origin: String!, destination: String!, waypoints: Array<String>!, mode: String!, completionHandler: ((_ status: String, _ success: Bool) -> Void)?) {
if let originLocation = origin {
if let destinationLocation = destination {
var directionsURLString = baseURLDirections + "origin=" + originLocation + "&destination=" + destinationLocation + "&mode=" + mode
if let routeWaypoints = waypoints {
directionsURLString += "&waypoints=optimize:true"
for waypoint in routeWaypoints {
directionsURLString += "|" + waypoint
}
}
directionsURLString = ("\(directionsURLString)&sensor=true&key=???????????????")
print("directons*******")
print(directionsURLString)
directionsURLString = directionsURLString.addingPercentEscapes(using: String.Encoding.utf8)!
let directionsURL = NSURL(string: directionsURLString)
DispatchQueue.main.async( execute: { () -> Void in
let directionsData = NSData(contentsOf: directionsURL! as URL)
do{
let dictionary: Dictionary<String, AnyObject> = try JSONSerialization.jsonObject(with: directionsData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String, AnyObject>
let status = dictionary["status"] as! String
let json = JSON(data: directionsData as! Data)
if status == "OK" {
self.selectedRoute = (dictionary["routes"] as! Array<Dictionary<String, AnyObject>>)[0]
self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<String, AnyObject>
let legs = self.selectedRoute["legs"] as! Array<Dictionary<String, AnyObject>>
let startLocationDictionary = legs[0]["start_location"] as! Dictionary<String, AnyObject>
self.originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double)
let endLocationDictionary = legs[legs.count - 1]["end_location"] as! Dictionary<String, AnyObject>
self.destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double)
let originAddress = legs[0]["start_address"] as! String
let destinationAddress = legs[legs.count - 1]["end_address"] as! String
for (index, leg) in json["routes"][0]["legs"].arrayValue.enumerated() {
var count = 0
//MARK: Start Building Directions Array
for (stepIndex, step) in json["routes"][0]["legs"][index]["steps"].arrayValue.enumerated() {
count += 1
let htmlInstructions = json["routes"][0]["legs"][index]["steps"][stepIndex]["html_instructions"].string
let distance = json["routes"][0]["legs"][index]["steps"][stepIndex]["distance"]["text"].string
let duration = json["routes"][0]["legs"][index]["steps"][stepIndex]["duration"]["text"].string
let direction:Direction = Direction(index: count, htmlInstructions: htmlInstructions, distance: distance, duration: duration)
self.directions.append(direction)
}
self.tableView.reloadData()
}
//end of stepts to get writtine directions
//NOT Plotting markers endpoins
//position markers for ployline endpoints
//let originMarker = GMSMarker(position: self.originCoordinate)
// originMarker.map = self.mapView
//originMarker.icon = UIImage(named: "mapIcon")
// originMarker.title = originAddress
self.destinationMarker = currentDestination
// destinationMarker.map = self.mapView
// destinationMarker.icon = UIImage(named: "mapIcon")
// destinationMarker.title = destinationAddress
// destinationMarker.icon = GMSMarker.markerImage(with: UIColor.green)
if waypoints != nil && waypoints.count > 0 {
for waypoint in waypoints {
let lat: Double = (waypoint.components(separatedBy: ",")[0] as NSString).doubleValue
let lng: Double = (waypoint.components(separatedBy: ",")[1] as NSString).doubleValue
let marker = GMSMarker(position: CLLocationCoordinate2DMake(lat, lng))
marker.map = self.mapView
marker.icon = UIImage(named: "mapIcon")
}
}
self.routePolyline.map = nil
let route = self.overviewPolyline["points"] as! String
let path: GMSPath = GMSPath(fromEncodedPath: route)!
self.routePolyline = GMSPolyline(path: path)
self.routePolyline.map = self.mapView
self.routePolyline.strokeColor = UIColor.red
self.routePolyline.strokeWidth = 3.0
//Fit map to entire polyline
var bounds = GMSCoordinateBounds()
for index in 1...path.count() {
bounds = bounds.includingCoordinate(path.coordinate(at: index))
}
self.mapView.animate(with: GMSCameraUpdate.fit(bounds))
// end of fit map to ployline
}
else {
print("status of poly draw")
//completionHandler(status: status, success: false)
}
}
catch {
print("catch")
// completionHandler(status: "", success: false)
}
})
}
else {
print("Destination is nil.")
//completionHandler(status: "Destination is nil.", success: false)
}
}
else {
print("Origin is nil")
//completionHandler(status: "Origin is nil", success: false)
}
}
I added a return (->([Direction]))to the closure and was able to access the data outside of it.
func getDirections(currentDestination: GMSMarker, origin: String!, destination: String!, waypoints: Array<String>!, mode: String!, completionHandler: ((_ status: String, _ success: Bool) -> Void)?) -> ([Direction])
I'm new so not sure if its best practice to do it that way, but I needed data to display in a table.
I try to get start with swiftyJSON and parsing information in Swift so i start with this API to understand it:
http://api.androidhive.info/contacts/
So here is my Code
import Fuzi
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
var arrRes = [[String:AnyObject]]() //Array of dictionary
var JSON_TestURL = "http://api.androidhive.info/contacts/"
override func viewDidLoad() {
super.viewDidLoad()
//get JSON
Alamofire.request(.GET, JSON_TestURL).responseJSON { (responseData) -> Void in
let swiftyJsonVar = JSON(responseData.result.value!)
//print(swiftyJsonVar)
//get JSONarray Name and write it into new Array
if let resData = swiftyJsonVar["contacts"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
//count array and give "tag"
print(self.arrRes.count)
for var index = 0; index < self.arrRes.count; ++index {
var dict = self.arrRes[index]
let x = dict["name"] as? String
print(x)
print("")
}
}
}
getting the name, id etc is no problem but when i try to get the phone number i can't find a way.
for var index = 0; index < self.arrRes.count; ++index {
var dict = self.arrRes[index]
let x = dict["phone"]!["mobile"] as? String
print(x)
//print("")
}
When using SwiftyJSON you don't have to cast the objects like you do with NSJSONSerialization. You don't have to use this way of looping either when a simple call to map will do the trick.
For example, replace all this:
if let resData = swiftyJsonVar["contacts"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
print(self.arrRes.count)
for var index = 0; index < self.arrRes.count; ++index {
var dict = self.arrRes[index]
let x = dict["phone"]!["mobile"] as? String
print(x)
}
with this:
let contacts = swiftyJsonVar["contacts"].arrayValue
let allMobilePhones = contacts.map { $0["phone"]["mobile"].stringValue }
print(allMobilePhones)
I am having some troubles with some json data. I'm making a weather app and most of the information I parsed works but the weather
this is the council output for the json data as a whole this is the section im having troubles with
the full output of json
{
base = stations;
clouds = {
all = 90;
};
cod = 200;
coord = {
lat = "39.74";
lon = "-104.98";
};
dt = 1427305893;
id = 5419384;
main = {
humidity = 84;
pressure = 1022;
temp = "274.07";
"temp_max" = "275.35";
"temp_min" = "272.15";
};
name = Denver;
rain = {
1h = "0.25";
};
snow = {
1h = "0.17";
};
sys = {
country = US;
id = 532;
message = "0.07829999999999999";
sunrise = 1427288058;
sunset = 1427332632;
type = 1;
};
visibility = 4023;
weather = (
{
description = "light rain";
icon = 10d;
id = 500;
main = Rain;
},
{
description = snow;
icon = 13d;
id = 601;
main = Snow;
},
{
description = fog;
icon = 50d;
id = 741;
main = Fog;
},
{
description = mist;
icon = 50d;
id = 701;
main = Mist;
}
);
wind = {
deg = 20;
gust = "14.9";
speed = "12.9";
};
}
i also have it print the keys
[base, id, dt, snow, main, coord, sys, wind, weather, visibility, clouds, cod, name, rain]
I tried to save it as an array but when I set the arry[0] to a string it crashes
my code for the function
func populateLabels(weatherData: NSData){
var jsonError: NSError?
let json = NSJSONSerialization.JSONObjectWithData(weatherData, options: nil, error: &jsonError) as NSDictionary
println(json)
if let city = json["name"] as? String {
CityName.text = city
}
println(json.allKeys)
if let coord = json["coord"] as? NSDictionary {
if let longi = coord["lon"] as? Double {
long.text = String(format: "%.2f", longi)
}
if let lati = coord["lat"] as? Double {
lat.text = String(format: "%.2f", lati)
}
}
if let main = json["main"] as? NSDictionary {
if let temper = main["temp"] as? Double {
temp.text = String(format: "%.2f", temper)
}
}
If anyone knows how to get to the description that would be awesome
thanks js
I got it.. thanks for the help blacksquare, larme, chirag90
if let tasks = json["weather"] as? NSArray
{
if let task = tasks[0] as? NSDictionary
{
if let taskName = task["description"] as? NSString
{
println(taskName)
}
}
}