Swift 4 - Invalid conversion from throwing function - ios

I have connect.swift with code:
public struct Connect {
let adresSerwera = "http://test.nazwa.pl/"
typealias Odpowiedz = (Data?, Error?) -> Void
func getJsonFromServer(parametry: String, wynikRequesta: #escaping Odpowiedz) {
guard let Url = URL(string: self.adresSerwera + "kartyEndpoint.qbpage" + parametry) else { return }
URLSession.shared.dataTask(with: Url) { (data, response, error) in
if error == nil {
guard let data = data else {
print("Error 100: \(error)")
wynikRequesta(nil, error)
return
}
print("R>" + self.adresSerwera + "kartyEndpoint.qbpage?" + parametry)
do {
//let json = try JSONDecoder().decode(forecast.self, from: data)
wynikRequesta(data, nil)
dump(data)
print("\(data)")
} catch let err {
print("Error 101: ", err)
wynikRequesta(nil, err)
}
} else{
print("Error 102: Problem with download data")
}
}.resume()
}
func sprawdzDaneLogowania(login: String?, haslo: String?, callback: #escaping Odpowiedz) {
getJsonFromServer(parametry: "?action=LOGOWANIE&login=\(login!)&password=\(haslo!)", wynikRequesta: callback)
}
}
and code to download data:
#IBAction func btnLoginPressed(_ sender: Any) {
if self.textFieldLogin.text?.isEmpty ?? true || self.textFieldPassword.text?.isEmpty ?? true {
print("Uzupełnij wszystkie pola!!")
} else {
print("Pola uzupełnione")
cms.sprawdzDaneLogowania(login: self.textFieldLogin.text, haslo: self.textFieldLogin.text, callback: { (data, blad) in
if blad == nil{
if let dane = data {
let str = String(data: dane, encoding: .utf8)
let downloadedData = RankingGrupowyObject(JSONString: str!)
let decoder = JSONDecoder()
let zalogowanyUser = try decoder.decode(LoginUser.self, from: data)
} else {
print("Error 103: \(data)")
}
} else {
print("Error 104: \(blad)")
}
})
}
}
for lines:
cms.Check the Logs (login: self.textFieldLogin.text, password: self.textFieldLogin.text, callback: {(date, error) in
I get an error message:
Invalid conversion from throwing function of '(_, _) throws -> ()' is
a non-throwing function type 'Connect. Answer' (aka '(Optional ,
Optional ) -> ()')
What have I done wrong? How can you fix this error?
By using the CheckLogging function I would like to create a target object

let zalogowanyUser = try decoder.decode(LoginUser.self, from: data)
This part is able to throw, meaning that you should either do/catch there:
do {
let zalogowanyUser = try decoder.decode(LoginUser.self, from: data)
}
catch {
print("Error in decoder")
}
Or let the error propagate onto upper parts. In order to do that, your method cms.sprawdzDaneLogowania could be marked as throws, or your that method's callback block could be marked as such.

Related

How to receive an output for a method from a closure in Swift?

How to receive an output of an array of [TweetSentimentClassifierInput] objects to send it further to my prediction model?
I have the array but inside a closure which turns it unavailable to return as a method output. If I initialize an empty array outside the closure then the output is always an empty array since fetch closure takes time to be completed.
Code
struct TweetFetcher {
let tweetCount = 100
let swifter = Swifter(consumerKey: key, consumerSecret: secret)
func fetchTweets(with searchText: String) -> [TweetSentimentClassifierInput] {
swifter.searchTweet(using: searchText, lang: "en", count: tweetCount, tweetMode: .extended) {(results, searchMetadata) in
var tweets = [TweetSentimentClassifierInput]()
let data = results.description.data(using: .utf8)
do {
let decodedData = try JSONDecoder().decode([TweetData].self, from: data!)
} catch {
print("Error with decoding, \(error)")
}
for tweet in decodedData {
let tweetForClassification = TweetSentimentClassifierInput(text: tweet.full_text)
tweets.append(tweetForClassification)
}
} failure: { (error) in
print("Error with the Twitter API request, \(error)")
}
}
}
How can I return a non-empty array from a closure as a method output?
You should use a completionHandler concept to achieve async operations like this:
struct TweetFetcher {
let tweetCount = 100
let swifter = Swifter(consumerKey: key, consumerSecret: secret)
func fetchTweets(with searchText: String, completion: #escaping ([TweetSentimentClassifierInput]?, Error?) -> Void) {
swifter.searchTweet(using: searchText, lang: "en", count: tweetCount, tweetMode: .extended) {(results, searchMetadata) in
var tweets = [TweetSentimentClassifierInput]()
let data = results.description.data(using: .utf8)
do {
let decodedData = try JSONDecoder().decode([TweetData].self, from: data!)
} catch {
print("Error with decoding, \(error)")
completion(nil, error)
}
for tweet in decodedData {
let tweetForClassification = TweetSentimentClassifierInput(text: tweet.full_text)
tweets.append(tweetForClassification)
}
completion(tweets, nil)
} failure: { (error) in
print("Error with the Twitter API request, \(error)")
completion(nil, error)
}
}
}
Usage
let fetcher = TweetFetcher()
fetcher.fetchTweets(with: "Keyword...") { tweets, error in
if let error = error {
print(error.localizedDescription)
} else {
// Use tweets array content here ...
}
}
Convert this method in async, passing closure with [TweetSentimentClassifierInput] as closure argument, and an error as secondary closure argument,
func fetchTweets(with searchText: String, finished: ((_ sentiments: [TweetSentimentClassifierInput]?,_ error: Error?) -> Void)) {
swifter.searchTweet(using: searchText, lang: "en", count: tweetCount, tweetMode: .extended) {(results, searchMetadata) in
var tweets = [TweetSentimentClassifierInput]()
let data = results.description.data(using: .utf8)
do {
let decodedData = try JSONDecoder().decode([TweetData].self, from: data!)
} catch {
print("Error with decoding, \(error)")
}
for tweet in decodedData {
let tweetForClassification = TweetSentimentClassifierInput(text: tweet.full_text)
tweets.append(tweetForClassification)
}
finished(tweets, nil)
} failure: { (error) in
print("Error with the Twitter API request, \(error)")
finished(nil, error)
}
}

How can i make my Networking class generic in Swift?

Here i am extracting data as DataModel. But i want to make this class generic and pass the model myself so that i can use it to parse data from multiple API's. Can Anyone Help?
import Foundation
struct NetworkManager {
func fetchData(url : String, completion : #escaping (DataModel?) -> ()) {
print("Neeraj here")
let sessionURL = URL(string: url)
let session = URLSession(configuration: .default)
let dataTask = session.dataTask(with: sessionURL!) { (data, response, error) in
if error == nil {
if let safeData = data {
if let parsedData = self.parseData(data : safeData) {
print("got data")
completion(parsedData)
}
else {
debugPrint("failed to fetch data")
completion(nil)
}
}
}
else {
print("error in data task is \(String(describing: error))")
completion(nil)
}
}
dataTask.resume()
}
func parseData(data : Data) -> DataModel? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(DataModel.self, from: data)
return decodedData
} catch {
print("error while parsing data \(error)")
return nil
}
}
}
With the convenient Result type you can write a quite tiny generic method, it returns the decoded type on success and any error on failure
func fetchData<T: Decodable>(urlString: String, completion: #escaping (Result<T,Error>) -> Void) {
guard let url = URL(string: urlString) else { return } // or throw an error
URLSession.shared.dataTask(with: url) { (data, _, error) in
if let error = error { completion(.failure(error)); return }
completion( Result{ try JSONDecoder().decode(T.self, from: data!) })
}.resume()
}
Note: Force unwrapping data! is 100% safe if no error occurs
Be aware that you have to specify the concrete type when you are going to call the method
fetchData(urlString: "https://example.com/api") { (result : Result<MyModel,Error>) in
switch result {
case .success(let model): print(model)
case .failure(let error): print(error)
}
}
You can add a generic type constraint (called Model) which conforms Decodable like below:
struct NetworkManager {
func fetchData<Model: Decodable>(url : String, completion : #escaping (Model?) -> ()) {
let sessionURL = URL(string: url)
let session = URLSession(configuration: .default)
let dataTask = session.dataTask(with: sessionURL!) { (data, response, error) in
if error == nil {
if let safeData = data {
do {
let decodedData = try JSONDecoder().decode(Model.self, from: safeData)
completion(decodedData)
} catch {
print("error while parsing data \(error)")
}
} else {
debugPrint("failed to fetch data")
completion(nil)
}
}
else {
print("error in data task is \(String(describing: error))")
completion(nil)
}
}
dataTask.resume()
}
}
Usage
struct SampleModel: Decodable {
let name: String
}
NetworkManager().fetchData(url: "") { (data: SampleModel?) in
print(data)
}
You can write a generic function to fetch data like this one :
func fetchGenericData<T: Decodable>(urlString: String, completion: #escaping (T) -> ()) {
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!) { (data, resp, err) in
if let err = err {
print("Failed to fetch data:", err)
return
}
guard let data = data else { return }
do {
let obj = try JSONDecoder().decode(T.self, from: data)
completion(obj)
} catch let jsonErr {
print("Failed to decode json:", jsonErr)
}
}.resume()
}
}
I suppose that you have a data model, if you have not, you should create for your every object. Also by using a dummy URL i will make a request and fetch the JSON includes some users name and ids with JSON format.
Let`s define a data model for this:
struct StackUser: Decodable {
let id: Int
let name: String
}
fetchGenericData(urlString: "https://api.stackoverexample.com/stackusers") { (stackUsers: [StackUser]) in
stackUsers.forEach({print($0.name)})
}
Finally you will be parse the data and prints like this:
Rob
Matt
Vadian

Parse image from web json

I have a json file that looks something like this:
{
"adTitle": "My Title",
"adURL": "https://mylink.com/",
"adImageURL": "http://mywebsite/bannerx#3x.png"
}
I get the JSON value from website: http://mywebsite.com/file.json
The problem is that the ad somehow doesn't load the adImageURL, so when I press the UIImageView, but when I press the area that then UIImageView should be, it open my adURL. This is the code I use for JSON:
var imageURL:String = "http://mywebsite/bannerx#3x.png"
var adURL:String = "https://mylink.com/"
func loadAdvertisement() {
// Set up the URL request
let todoEndpoint: String = "http://mywebsite.com/file.json"
guard let url = URL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = URLRequest(url: url)
// set up the session
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
// print("error calling GET on /todos/1")
// print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// parse the result as JSON, since that's what the API provides
do {
guard (try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: AnyObject]) != nil else {
print("error trying to convert data to JSON")
return
}
let json = try JSONSerialization.jsonObject(with: responseData, options:.allowFragments) as! [String:AnyObject]
if (json != nil) {
self.imageURL = (json["adImageURL"] as? String)!
self.adURL = (json["adURL"] as? String)!
print(self.imageURL)
print(self.adURL)
DispatchQueue.main.async { () -> Void in
self.loadAdImage(self.imageURL)
}
}
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
// let jsonURL = URL(string: "http://mywebsite.com/file.json")
// self.getDataFromUrl(jsonURL!, completion: (data:Data?, response:URLResponse?, error:Error?)) -> Void
}
func loadAdImage(_ url:String) {
getDataFromUrl(URL(string: url)!) { (data, response, error) in
DispatchQueue.main.async { () -> Void in
guard let data = data, error == nil else { return }
print(response?.suggestedFilename ?? "")
print("Download Finished")
self.advertImageView.image = UIImage(data: data)
}
}
}
func getDataFromUrl(_ url:URL, completion: #escaping ((_ data: Data?, _ response: URLResponse?, _ error: NSError? ) -> Void)) {
URLSession.shared.dataTask(with: url) { (data:Data?, response:URLResponse?, error:Error?) in
completion(data, response, error as NSError?)
}.resume()
}
In the event LOG, is prints out both of the print("error trying to convert data to JSON") commands. I have used this code before in my project, and it worked just fine, but I have no idea why it wont work anymore.
Add the message to catch and check what actually error you are getting like this way:
do {
let json = try JSONSerialization.jsonObject(with: responseData, options:.allowFragments) as! [String:AnyObject]
} catch let message {
print("error trying to convert data to JSON" + "\(message)")
return
}

Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()'

I created a function to retrieve json data from a network server. It works properly when run in a production environment but when it's setup to be unit-tested the error : Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()'. Here is the function:
func getJSONData <T : Codable> (url: ServerUrl, type: T.Type, completionHandler:#escaping (_ details: T) -> ()) {
guard let url = URL(string: baseURL + url.rawValue) else { return }
session.dataTask(with: url, completionHandler: {
(data, response, err) in
guard err == nil else {
print("There was a network error:", err as Any)
return
}
guard let data = data else { return }
do {
let updatedModels = try JSONDecoder().decode(T.self, from: data)
completionHandler(updatedModels)
} catch let jsonErr {
print("Error serializing json:", jsonErr)
return }
}).resume()
}
Here it is in the unit test:
func test_getJSONData_should_return_data() {
var actualData: Data?
sut.getJSONData(url: ServerUrl.colors, type: MainColor.self, completionHandler: { (data) in //error here
actualData = data
})
XCTAssertNotNil(actualData)
}
The problem is that completion returns value of type MainColor not Data , this should be
var actualData: MainColor?

Alamofire 3->4 trouble with Reponse & ResponseSerializer Swift 3.0

I'm having trouble with the ResponseSerializer I get an unresolved identifier and for Response I get an undeclared type. I've read from alamofire migration doc that Response has been changed to multiple types. So I should change Response->DataReponse but this means I can only pass one argument like:
// What I have
Response(<ListWrapper, NSError>)
// What I should change it to?
DataResponse(<ListWrapper>)
How can I still recieve the Error this way and more importantly how do I migrate the extension to alamofire 4?
My class:
class List{
var idNumber: String?
var title: String?
var posterPath: String?
var release: String?
required init(json: JSON, id: Int?)
{
self.idNumber = json[ListFields.Id.rawValue].stringValue
self.title = json[ListFields.Title.rawValue].stringValue
self.posterPath = json[ListFields.PosterPath.rawValue].stringValue
self.release = json[ListFields.Release.rawValue].stringValue
}
class func setURL_APPEND(_ url: String)
{
URL_APPEND = url
}
// MARK: Endpoints
class func endpointForList() -> String
{
return URL_APPEND
}
fileprivate class func getListAtPath(_ path: String, completionHandler: #escaping (ListWrapper?, NSError?) -> Void) {
Alamofire.request(path)
.responseListArray { response in
if let error = response.result.error
{
completionHandler(nil, error)
return
}
completionHandler(response.result.value, nil)
}
}
class func getList(_ completionHandler: #escaping (ListWrapper?, NSError?) -> Void)
{
getListAtPath(List.endpointForList(), completionHandler: completionHandler)
}
}
// Problem is here:
// for ResponseSerializer I get an unresolved identifier
// and for Response I get an undeclared type
extension Alamofire.Request {
func responseListArray(_ completionHandler: #escaping (Response<ListWrapper, NSError>) -> Void) -> Self {
let responseSerializer = ResponseSerializer<ListWrapper, NSError> { request, response, data, error in
guard error == nil else
{
return .failure(error!)
}
guard let responseData = data else {
let failureReason = "Array could not be serialized because input data was nil."
let error = Alamofire.Error.errorWithCode(.dataSerializationFailed, failureReason: failureReason)
return .failure(error)
}
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)
switch result {
case .success(let value):
let json = SwiftyJSON3.JSON(value)
let wrapper = ListWrapper()
var allList:Array = Array<List>()
wrapper.totalCount = json["favorite_count"].intValue
// print(json)
let results = json["items"]
// print(results)
for jsonList in results
{
//print(jsonList.1)
let list = List(json: jsonList.1, id: Int(jsonList.0) )
if (list.posterPath == "")
{
continue
}
else
{
//print(movies.posterPath)
allList.append(list)
}
}
wrapper.results = allList
return .success(wrapper)
case .failure(let error):
return .failure(error)
}
}
return response(responseSerializer: responseSerializer,completionHandler: completionHandler)
}
}
Bro try below code see:
func responseListArray(_ completionHandler: #escaping (Response<ListWrapper>) -> Void) -> Self {
let responseSerializer = ResponseSerializer<ListWrapper> { request, response, data, error in
guard error == nil else
{
return .failure(error!)
}
guard let responseData = data else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))
}
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)
switch result {
case .success(let value):
let json = SwiftyJSON3.JSON(value)
let wrapper = ListWrapper()
var allList:Array = Array<List>()
wrapper.totalCount = json["favorite_count"].intValue
// print(json)
let results = json["items"]
// print(results)
for jsonList in results
{
//print(jsonList.1)
let list = List(json: jsonList.1, id: Int(jsonList.0) )
if (list.posterPath == "")
{
continue
}
else
{
//print(movies.posterPath)
allList.append(list)
}
}
wrapper.results = allList
return .success(wrapper)
case .failure(let error):
return .failure(error)
}
}
return response(responseSerializer: responseSerializer,completionHandler: completionHandler)
}

Resources