Expression implicity coerced from "error?" to Any - swift

I have this error:
Expression implicity coerced from "Error?" to Any
and I am not sure what it means, hoping to find help.
let task = URLSession.shared.dataTask(with: url) { (data,response, error) in
if error != nil {
print(error)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers)
print(jsonResult)
} catch {
print("JSON Processing Failed")
}
}
}
}
task.resume()

Check this line:
print(error)
You are printing an optional value. You should unwrap it with if let.
if let error = error {
print(error)
}

Related

Could not cast value of type 'Swift.String' (0x10fef45c0) to 'Swift.Error' (0x10ff2bd10). (lldb)

Below line of code is producing the error,
DispatchQueue.main.async {
completion(.success(jsonData), Error as! Error)
}
When print jsonData This code returns perfect result of array but getting this error,
Could not cast value of type 'Swift.String' (0x10fef45c0) to 'Swift.Error' (0x10ff2bd10). (lldb)
As the error says I understand its a cast exception, but I'm not able to modify the code to make it work. I'm kinda new to Swift, so any help would be appreciated. Below is my
import Foundation
class APIService {
private var dataTask: URLSessionDataTask?
func getPopularPosts(completion: #escaping (Result<Any, Error>, Error) -> Void) {
let popularURL = "URL Here"
guard let url = URL(string: popularURL) else {return}
// Create URL Session - work on the background
dataTask = URLSession.shared.dataTask(with: url) { (data, response, error) in
// Handle Error
if let error = error {
completion(.failure(error), Error.self as! Error)
print("DataTask error: \(error.localizedDescription)")
return
}
guard let response = response as? HTTPURLResponse else {
// Handle Empty Response
print("Empty Response")
return
}
print("Response status code: \(response.statusCode)")
guard let data = data else {
// Handle Empty Data
print("Empty Data")
return
}
do {
// Parse the data
let decoder = JSONDecoder()
let jsonData = try decoder.decode(APIService.self, from: data)
// print(jsonData)
// Back to the main thread
DispatchQueue.main.async {
completion(.success(jsonData), Error as! Error)
}
} catch let error {
completion(.failure(error),error)
}
}
dataTask?.resume()
}
}
Modify the completion block parameters, you already are returning the error inside the Result's .failure(Error) block so no need to repeat it again as another parameter in the completion parameter. Here's how you fix this:
Declaration:
class APIService {
private var dataTask: URLSessionDataTask?
func getPopularPosts(completion: #escaping (Result<CategoriesNewsData, Error>) -> Void) {
let popularURL = "URL Here"
guard let url = URL(string: popularURL) else {return}
// Create URL Session - work on the background
dataTask = URLSession.shared.dataTask(with: url) { (data, response, error) in
// Handle Error
if let error = error {
completion(.failure(error))
print("DataTask error: \(error.localizedDescription)")
return
}
guard let response = response as? HTTPURLResponse else {
// Handle Empty Response
print("Empty Response") // Throw a custom error here too.
return
}
print("Response status code: \(response.statusCode)")
guard let data = data else {
// Handle Empty Data
print("Empty Data") // Throw a custom error here too.
return
}
do {
let decoder = JSONDecoder()
let jsonData = try decoder.decode(CategoriesNewsData.self, from: data)
DispatchQueue.main.async {
completion(.success(jsonData))
}
} catch let error {
completion(.failure(error))
}
}
dataTask?.resume()
}
}
Calling:
service.getPopularPosts { result in
switch result {
case .success(let categoriesNewsData):
print(categoriesNewsData)
case .failure(let error):
print(error)
}
}

Swift Function Not Continuing after completion of Json Decoder within - "else if" issue?

I am using this function to call json information from a database - it gets the information fine.
But it does not continue after the "getHistoricalMonthlyData". so it will not get to the print("****** line 55"). (yes, I plan on making this a func once I figure out the issue.)
it will print the "print(i.stock)" fine.
I can share the "getHistoricalMonthlyData" code but it works fine and I doubt that is the issue.
I am not great with the completion handlers and I suspect that is the issue?
below is the "getHistoricalMonthlyData" function that I can not get past.
func calculateMonthPerformance (setting: settings) {
let set = setting
let u = User.getUser()
var i = Indexes()
getHistoricalMonthlyData(symbol: symbol, beg: set.monthBeg, end: set.monthEnd) { (json, error ) in
if let error = error {
print ("error", error)
} else if let json = json {
print ("success")
i.stock = json
print(47)
}
// this is fine
print(50)
print(i.stock)
}
// nothing at this point
print("****** line 55")
}
This is how the json function is set up and works great in another project.
it has a resume.
func getHistoricalMonthlyData(symbol: String, beg: Date, end: Date, completionHandler: #escaping ([HistoricalData]?, Error?) -> Void) {
let beg = beg.dateAtStartOf(.month).toFormat("yyyy-MM-dd")
let end = end.dateAtEndOf(.month).toFormat("yyyy-MM-dd")
let jsonUrl = "https://eodhistoricaldata.com/api/eod/\(symbol).US?from=\(beg)&to=\(end)&api_token=\(EOD_KEY)&period=eom&fmt=json"
guard let url = URL(string: jsonUrl) else {
print("Error: cannot create URL")
let error = BackendError.urlError(reason: "Could not create URL")
completionHandler(nil, error)
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil else {
completionHandler(nil, error!)
return
}
guard let jasonData = data else {
print("Error: did not receive data")
let error = BackendError.objectSerialization(reason: "No data in response")
completionHandler(nil, error)
return
}
do {
let historical = try JSONDecoder().decode([HistoricalData].self, from: jasonData )
completionHandler(historical, nil)
} catch let jsonErr {
print ("Error serializing json", jsonErr )
let error = BackendError.objectSerialization(reason: "Couldn't create a todo object from the JSON")
completionHandler(nil, error)
}
}.resume()
}
thanks.
if someone knows a better answer would love to hear about it.
I added a DispatchSemaphore to the code and it seems to work.
cheers.
let semaphore = DispatchSemaphore(value: 0)
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil else {
completionHandler(nil, error!)
return
}
guard let jasonData = data else {
let error = BackendError.objectSerialization(reason: "No data in response")
completionHandler(nil, error)
return
}
do {
let historical = try JSONDecoder().decode([HistoricalData].self, from: jasonData )
completionHandler(historical, nil)
} catch let jsonErr {
let error = BackendError.objectSerialization(reason: "Couldn't create a todo object from the JSON")
completionHandler(nil, error)
}
semaphore.signal()
}.resume()
_ = semaphore.wait(timeout: .distantFuture)

Why isn't my Weather Underground data not printing in Swift 3?

I'm having an issue printing data from Weather Underground. My code works with other data sources, just not Weather Underground. I have even tried replacing the URL with actual data (i.e. https://api.wunderground.com/api/APIKEY/forecast/geolookup/forecast/q/94129.json"), but it doesn't print.
Any suggestions on what it could be?
import Foundation
import UIKit
class APIManager {
func weatherJSON(zip: String, completion: #escaping ([Weather]) -> Void) {
let baseUrlString = "https://api.wunderground.com/api/APIKEY/forecast/geolookup/forecast/q/\(zip).json"
guard let url = URL(string: baseUrlString) else { return }
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil, let data = data else { return }
do {
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] else { return }
// MARK: Print JSON
print(json)
var weatherList = [Weather]()
for item in json {
if let weather = Weather.create(from: item) {
weatherList.append(weather)
}
}
completion(weatherList)
} catch {
print("Uh oh. You have an error with \(zip)!")
}
}
task.resume()
}
}
EDIT: SOLVED
I have used the code posted below and am now seeing errors.
I'd suggest changing this to report errors:
enum WeatherError Error {
case badURL
case invalidJSON
}
func weatherJSON(zip: String, completion: #escaping ([Weather]?, Error?) -> Void) {
let baseUrlString = "https://api.wunderground.com/api/APIKEY/forecast/geolookup/forecast/q/\(zip).json"
guard let url = URL(string: baseUrlString) else {
completion(nil, WeatherError.badURL)
return
}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil, let data = data else {
completion(nil, error)
return
}
do {
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] else {
completion(nil, WeatherError.invalidJSON)
return
}
// MARK: Print JSON
print(json)
var weatherList = [Weather]()
for item in json {
if let weather = Weather.create(from: item) {
weatherList.append(weather)
}
}
completion(weatherList, nil)
} catch let parseError {
print("Uh oh. You have an error with \(zip)!")
if let responseString = String(data: data, encoding: .utf8) {
print("responseString = \(responseString)")
}
completion(nil, parseError)
}
}
task.resume()
}
Then, when you call it, you can see what the error was
weatherJSON(zip: something) { weatherReports, error in
guard let weatherReports = weatherReports, error == nil else {
print(error)
return
}
// use weatherReports here
}
This won't solve your problem, but it will help you diagnose what the issue is.

Cannot subscript a value of type 'AnyObject' with an index of type 'String'

i tried to get The (Name) value to a label
i used resultLabel.text = !(jsonResult["name"]) but it returns an error
Cannot subscript a value of type 'AnyObject' with an index of type 'String'
See my JSON
and does anybody know's how to get the data..
My code ....
if let url = URL(string: "http://www.omdbapi.com/?t=The+Raid&y=&plot=short&r=json") {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in // URLSession.shared().dataTask(with: url) { (data, response, error) is now URLSession.shared.dataTask(with: url) { (data, response, error)
if error != nil {
print(error)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject // Added "as anyObject" to fix syntax error in Xcode 8 Beta 6
print(jsonResult)
print(jsonResult["Title"])
resultLabel.text = (jsonResult["name"])
if let description = ((jsonResult["weather"] as? NSArray)?[0] as? NSDictionary)?["description"] as? String {
DispatchQueue.main.sync(execute: {
self.resultLabel.text = description
})
}
} catch {
print("JSON Processing Failed")
}
}
}
}
task.resume()
} else {
resultLabel.text = "Couldn't find weather for that city - please try another."
}
}
Casting the result of JSON deserialization to AnyObject is the worst you can do.
First of all the unspecified JSON type is Any and since the type is supposed to be a dictionary, cast it to [String:Any].
Further in Swift 3 the compiler must know all types which are subscripted
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as! [String:Any]
let name = jsonResult["name"] as? String
print(name ?? "n/a")
if let weather = jsonResult["weather"] as? [[String:Any]], !weather.isEmpty {
if let description = weather[0]["description"] as? String {
DispatchQueue.main.async { // not sync !!
self.resultLabel.text = description
}
}
...
PS: As always, mutableContainers is meaningless in Swift and meaningless anyway if the values are only read.

Invalid conversion from throwing function of type Swift 2

I migrated my app to Swift 2 and I get the error
Invalid conversion from throwing function of type
on the following line:
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
Here's the full code block:
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
let image = UIImage(data: data!)
self.profilePic.image = image
if var realUser = user {
realUser["image"] = data as! AnyObject
try realUser.save()
FBRequestConnection.startForMeWithCompletionHandler({
connection, result, error in
realUser["first_name"] = result["first_name"]
realUser["last_name"] = result["last_name"]
try realUser.save()
})
}
})
How do I fix this code with the new catch implemented in Swift 2?
Thanks!
The block passed to sendAsynchronousRequest is not allowed to throw errors. So you have to catch the errors:
do {
try realUser.save()
}
catch {
// handle the error
}
Try this code:
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data in
let image = UIImage(data: data!)
self.profilePic.image = image
if var realUser = user {
realUser["image"] = data as! AnyObject
do {
try realUser.save()
}
catch {
print(error)
}
FBRequestConnection.startForMeWithCompletionHandler({
connection, result, error in
realUser["first_name"] = result["first_name"]
realUser["last_name"] = result["last_name"]
do {
try realUser.save()
}
catch {
print(error)
}
})
}
})