how to check SwiftyJSON if returl nil in link - swift

If my link does not return data how do I check in swiftyJSON,
I got this error : fatal error: unexpectedly found nil while unwrapping an Optional value
var URLString = mylink
URLString = URLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let url = NSURL(string: URLString)!
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, innerError) in
let jsonData = NSData(contentsOfURL: url)! as NSData? //>> here is the error fatal error: unexpectedly found nil while unwrapping an Optional value
let readableJson = JSON(data: jsonData!, options: NSJSONReadingOptions.MutableContainers, error: nil)
let jjson = readableJson[0]
let ID = jjson["Title"]
})
}
task.resume()

You should avoid the force unwrap "!".
Try this
func foo(urlAsString:String) {
guard let
urlAsStringEscaped = urlAsString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding),
url = NSURL(string: urlAsStringEscaped)
else { fatalError() }
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
if let data = data where error == nil {
let json = JSON(data: data)
let id = json[0]["Title"].string
}
}
task.resume()
}

Related

Swift, how can I return the data from HTTP request?

I have found learning swift to be more or less unbearable to do anything, something that would be done in a single line in Python becomes a whole task in swift.
I am trying to return the data from a http request and cannot find a single source that explains how. The only things I can find prints the data instead of returning it, either as a dictionary (from using JSONSerialization) or simply as a string.
let url = URL(string: "url")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if let error = error {
print("error: \(error)")
} else {
if let response = response as? HTTPURLResponse {
print("statusCode: \(response.statusCode)")
}
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print("data: \(dataString)")
}
}
}
task.resume()
func makePostRequest(){
let urlPath: String = "http://www.swiftdeveloperblog.com/http-post-example- script/"
var url: NSURL = NSURL(string: urlPath)!
var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
var stringPost="firstName=James&lastName=Bond" // Key and Value
let data = stringPost.dataUsingEncoding(NSUTF8StringEncoding)
request.timeoutInterval = 60
request.HTTPBody=data
request.HTTPShouldHandleCookies=false
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if (jsonResult != nil) {
// Success
println(jsonResult)
let message = jsonResult["Message"] as! NSString
println(message)
}else {
// Failed
println("Failed")
}
})
}

Fatal error: found nil - In calling Web API with JSON

Full error: Fatal error: Unexpectedly found nil while unwrapping an Optional value.
I am trying to get data from Web API service and am not sure where in the program it is getting nil value from.
Program crashes and getting error at line when declaring jsonResult
let urlAsString = "http://api.geonames.org/earthquakesJSON?north="+northString+"&south="+southString+"&east="+eastString+"&west="+westString+"&username=test"
let url = URL(string: urlAsString)!
let urlSession = URLSession.shared
let jsonQuery = urlSession.dataTask(with: url, completionHandler: { data, response, error -> Void in
if (error != nil) {
print(error!.localizedDescription)
}
var err: NSError?
let jsonResult = (try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSDictionary //program crashes and gets error here
if (err != nil) {
print("JSON Error \(err!.localizedDescription)")
}
print(jsonResult)
let setOne:NSArray? = jsonResult["earthquakes"] as? NSArray
print(setOne?[0]);
let y = setOne?[0] as? [String: AnyObject]
let dateTime: String = (y!["datetime"] as? NSString)! as String
DispatchQueue.main.async{
self.date.text = String(dateTime)
}
})
jsonQuery.resume()
Seems like data is nil. You forcibly tried to set nil value.
if let data = data {
if let jsonResult = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as NSDictionary {
if let setOne = jsonResult["earthquakes"] as? [NSDictionary] {
let y = setOne[0] as? [String: AnyObject]
let dateTime: String = (y!["datetime"] as? String)! as String
DispatchQueue.main.async{
self.date.text = String(dateTime)
}
}
}
}
It crashes because you force unwrap a nil value. So try with optional instead
replace this line let jsonResult = (try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSDictionary with:
do {
if let jsonResult = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary {
// Rest of your code here
}
} catch let error {
}

Can't get directions from GoogleMaps because of "found nil" unrapping URL

I searched through this topic and found some codes I tried to implement into my project, but it won't work!
So, what do I wanna achieve?
I wanna have a button in the UI, and when user tap the button, the app displays directions to a specific point on the GoogleMap. But my function crashes on the URL.
This is my code:
func draw(src: CLLocationCoordinate2D, dst: CLLocationCoordinate2D){
let urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=\(src)&destination=\(dst)&sensor=false&mode=driving&key=**API_KEY**" <- // Here I place API-Key
let url = URL(string: urlString) // Here is the crash!
URLSession.shared.dataTask(with: url!, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
}else{
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
let routes = json["routes"] as! NSArray
self.mapView.clear()
OperationQueue.main.addOperation({
for route in routes
{
let routeOverviewPolyline:NSDictionary = (route as! NSDictionary).value(forKey: "overview_polyline") as! NSDictionary
let points = routeOverviewPolyline.object(forKey: "points")
let path = GMSPath.init(fromEncodedPath: points! as! String)
let polyline = GMSPolyline.init(path: path)
polyline.strokeWidth = 3
let bounds = GMSCoordinateBounds(path: path!)
self.mapView!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 30.0))
polyline.map = self.mapView
}
})
}catch let error as NSError{
print("error:\(error)")
}
}
}).resume()
}
I don't know if the problem could be with the API-key, or if there's something else. I read that spaces etc could cause this issue, but I can't find what's wrong!
Error message:
Fatal error: Unexpectedly found nil while unwrapping an Optional value
2019-06-14 16:50:45 Fatal error: Unexpectedly found nil while unwrapping an Optional value
You wrongly create urlString with directly using CLLocationCoordinate2D as you have to use it's properties latitude/longitude
let urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=\(src)&destination=\(dst)&sensor=false&mode=driving&key=**API_KEY**" <- // Here I place API-Key
it should be
let urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=\(src.latitude),\(src.longitude)&destination=\(dst.latitude),\(dst.longitude)&sensor=false&mode=driving&key=**API_KEY**" <- // Here I place API-Key
Also it's better to avoid ! and do
guard let url = URL(string: urlString) else { return }

Thread 10: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I am trying to make a service call to get user details however i get this error :
Thread 10: Fatal error: Unexpectedly found nil while unwrapping an Optional value
From this code :
let urlString = "http://myURL.com/getInfo/getAccountTransactions/{accountPublicKey}"
print(urlString)
let requestUrl = URL(string:urlString)
let requestURL = URLRequest(url:requestUrl!)
When i wrap the code in a guard let the code doesn't get executed because it finds nil, i am not sure why because the URL string can never be nill since its initialized with a default value on the same code.
This the code in a guard let :
let urlString = "http://myURL.com/getInfo/getAccountTransactions/{accountPublicKey}"
guard let requestUrl = URL(string:urlString) else { return }
let requestURL = URLRequest(url:requestUrl)
This the entire service call code :
class TransactionServiceCall : NSObject, URLSessionDelegate{
let viewResponse = ThrowResponse()
func fetchTransactions(requestObject: Transaction, completion: #escaping (Dictionary<String,Any>?) -> Void) {
let urlString = "http://myURL.com/getInfo/getAccountTransactions/{accountPublicKey}"
guard let requestUrl = URL(string:urlString) else { return }
let requestURL = URLRequest(url:requestUrl)
let searchParams = Transaction.init(publicKey: requestObject.publicKey)
var request = requestURL
request.httpMethod = "POST"
request.httpBody = try? searchParams.jsonData()
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
do {
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if 200 ... 299 ~= statusCode {
if let json = try JSONSerialization.jsonObject(with: data!) as? Dictionary<String,Any> {
self.viewResponse.dismissLoader()
print(json)
completion(json)
}
}else{
self.viewResponse.dismissLoader()
self.viewResponse.showFailureAlert(title: "Failure", message: "")
completion(nil)
}
} catch {
DispatchQueue.main.async {
self.viewResponse.dismissLoader()
self.viewResponse.showFailureAlert(title: "Failure", message: "")
completion(nil)
}
}
})
task.resume()
}
}
It is important to note that the url has curly brackets in it e.g
http://myURL.com/getInfo/getAccountTransactions/{accountPublicKey}
You need to escape special characters in the url string using addingPercentEncoding(withAllowedCharacters:) and an appropriate CharacterSet so that a valid URL object may be created from it.
In your case, the CharacterSet should be .urlQueryAllowed
Like so:
//The unescaped string
let unescaped = "http://myURL.com/getInfo/getAccountTransactions/{accountPublicKey}"
//The escaped string
let escaped = unescaped.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
//...

Creating json swift app thing: fatal error: found nil when unwrapping json

I'm working on a json retrieving project or whatever, and I'm getting a fatal error while unwrapping a json. I think that the issue is that url is nil and I'm trying to unwrap it, but I'm not sure.
func getXRPData(urlString: String){
let url = NSURL(string: urlString)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in
dispatch_async(dispatch_get_main_queue(), {
self.setLabels(data)
})
}
task.resume()
}
func setLabels(xrpDataL: NSData){
var jsonError: NSError?
var percent = "%"
var dollarSIGN = "$"
let json = NSJSONSerialization.JSONObjectWithData(xrpDataL, options: nil, error: &jsonError) as! NSDictionary
//supply of xrp
if let supply = json["supply"] as? String{
var numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
if let result = numberFormatter.numberFromString(supply){
var finalResult = numberFormatter.stringFromNumber(result)
totalXRP.text = finalResult
}
}
Instead of force unwrapping, you should unwrap the results of JSONObjectWithData with an optional binding (if let) and a conditional downcast (as?):
if let json = NSJSONSerialization.JSONObjectWithData(xrpDataL, options: nil, error: &jsonError) as? NSDictionary {
// Continue parsing
} else {
// Handle error
}