Can't pass value? (CoreData and API-call) - swift

I have some trouble with part of my Swift Application. I am trying to make two requests to a REST API (Spark's API) and save the requested variables as CoreData. However, I can not get the values passed propably from one function to the other, even though it's all within the same class.
So here is how it should work.
The function startTimer calls a series of functions every 60 seconds.
var timer: dispatch_source_t!
//Calls the getData function twice every 60 seconds. First to get the Posturevalue and then to get the Time value.
func startTimer() {
let queue = dispatch_queue_create("que", nil)
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue)
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC, 1 * NSEC_PER_SEC)
dispatch_source_set_event_handler(timer) {
self.getData("Posturevalue")
let Posturevalue:Int = self.GSValHolder
self.getData("Time")
let Time:Int = self.GSValHolder
let CoreID:String = self.GSCoreID
self.saveCoreData(Time, PostureVal: Posturevalue, CoreID: CoreID)
}
dispatch_resume(timer)
}
First call is to the getData function, which sends an handles the JSON response. I know this is working, cause I can print the variables and call the set funtion.
//Sends a request to the URL to receive JSON data.
func getData(Variblename:String)
{
let dataurl:String = "https://api.spark.io/v1/devices/"+DeviceID+"/"+Variblename+"/?access_token="+Accesstoken
if let url = NSURL(string: dataurl) {
let request = NSURLRequest(URL: url)
initialiseTaskForGettingData(request, element: "results")
}
}
//Handles the JSON data. Get the DeviceID and Variblevalue (Posture Value and Time value)
func initialiseTaskForGettingData(request: NSURLRequest, element:String)
{
let task = session.dataTaskWithRequest(request) {data, response, downloadError in
if let error = downloadError
{
}
else
{
var parsingError: NSError?
let parsedResult: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parsingError)
if let coreData = parsedResult as? NSDictionary {
if let VariableVal = (coreData as NSDictionary)["result"] as? Int {
if let coreInfo = coreData["coreInfo"] as? NSDictionary {
if let deviceID = (coreInfo as NSDictionary)["deviceID"] as? String {
println(deviceID)
println(VariableVal)
self.GSValHolder = VariableVal
}
}
}
}
}
}
task.resume()
}
Then do I try to get the variable and assign it to a new variable. This part keep causing me trouble and won't work.
//Get/Set functions to pass AccessToken and CoreID from CoreInfoView to the Model.
var AccessToken:String!
var CoreID:String!
var GSAccessToken:String {
set (newAccessToken) {
self.AccessToken = newAccessToken
}
get {
return AccessToken
}
}
var GSCoreID:String {
set (newCoreID) {
self.CoreID = newCoreID
}
get {
return CoreID
}
}
var ValHolder:Int!
var GSValHolder:Int {
set (newVal) {
self.ValHolder = newVal
}
get {
return ValHolder
}
}
If it worked, the startTimer function would do the same over again in order to get the other variable. Finally I wan't to call the saveCoreData function and save the variables.
func saveCoreData(Time:Int, PostureVal:Int, CoreID:String) {
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "Coredata")
var newCore = NSEntityDescription.insertNewObjectForEntityForName("Coredata", inManagedObjectContext: context) as! NSManagedObject
newCore.setValue(Time, forKey: "time")
newCore.setValue(PostureVal, forKey: "posturevalue")
context.save(nil)
println(newCore)
}
Hope it all makes sense and you are able to help me out.
Regards Mads R. Svendsen,
________________________________UPDATE_________________________________________
So I did a bit of reading and watched some tutorials on completionHandling and asynchronous, which helped a lot. My code now looks like this and works.
var timer: dispatch_source_t!
//Calls the getData function twice every 60 seconds. First to get the Posturevalue and then to get the Time value.
func startTimer() {
let queue = dispatch_queue_create("que", nil)
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue)
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC, 1 * NSEC_PER_SEC)
dispatch_source_set_event_handler(timer) {
var Time:Int!
var Posturevalue:Int!
var CoreID:String!
self.getData("Posturevalue", completionHandler: { (result) -> Void in
if (result == true) {
Posturevalue = self.GSValHolder
self.getData("Time", completionHandler: { (result) -> Void in
if (result == true) {
Time = self.GSValHolder
CoreID = self.GSCoreID
self.saveCoreData(Time, PostureVal: Posturevalue, CoreID: CoreID)
}
})
}
})
}
dispatch_resume(timer)
}
// Temporary constants to compose the URL. Has to be replaced with the values passed on from the CoreInfoView.
let DeviceID:String = "51ff72065082554944270887"
let Accesstoken:String = "09f54420e11539de00c13284f9fec31d3fba410d"
let session = NSURLSession.sharedSession()
//Sends a request to the URL to receive JSON data.
func getData(Variblename:String, completionHandler: (result:Bool!) -> Void)
{
let dataurl:String = "https://api.spark.io/v1/devices/"+DeviceID+"/"+Variblename+"/?access_token="+Accesstoken
if let url = NSURL(string: dataurl) {
let request = NSURLRequest(URL: url)
initialiseTaskForGettingData(request, element: "results", completionHandler: { (result) -> Void in
if (result == true) {
completionHandler(result: true)
}
else {
completionHandler(result: false)
}
})
}
}
//Handles the JSON data. Get the DeviceID and Variblevalue (Posture Value and Time value)
func initialiseTaskForGettingData(request: NSURLRequest, element:String, completionHandler: (result:Bool!) -> Void)
{
let task = session.dataTaskWithRequest(request) {data, response, downloadError in
if let error = downloadError
{
completionHandler(result: false)
}
else
{
var parsingError: NSError?
let parsedResult: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parsingError)
if let coreData = parsedResult as? NSDictionary {
if let VariableVal = (coreData as NSDictionary)["result"] as? Int {
if let coreInfo = coreData["coreInfo"] as? NSDictionary {
if let deviceID = (coreInfo as NSDictionary)["deviceID"] as? String {
println(deviceID)
println(VariableVal)
self.GSValHolder = VariableVal
completionHandler(result: true)
}
}
}
}
}
}
task.resume()
}
However, I figured out that the dataTaskWithRequest can be written with a completionHandler, but I could not make it work. I was wondering if that might make the code a bit more simple?

Related

Cannot Assign the variables of a function to the Labels on Swift

I am working on an app that Decode a JSON file and creates three variables out of the function: Status,emptySlots,freeBikes. I want to assign these values to labels. However, no matter what I do, I was unable to get any output with any method.
The function code:
func getBikeData(stationName:String){
if let url = URL(string: "https://api.citybik.es//v2/networks/baksi-bisim"){
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data, error == nil else {
print(error?.localizedDescription ?? "Response Error")
return
}
do {
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
print(jsonResponse) //Response result
do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
//Decode JSON Response Data
let model = try decoder.decode(ResponseJSON.self, from: dataResponse)
print(model)//Output - 1221
if let station = model.network.stations.first(where: { $0.name == stationName }) {
//get the properties of station here as per requirement
let emptySlots: Int = station.empty_slots
let freeBikes: Int = station.free_bikes
let Status: String = station.extra.status
print(emptySlots, freeBikes, Status)
}
}
catch let parsingError {
print("Error", parsingError)
}
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
}
}
Any help is gladly appreciated. I have already tried to return the variables, and using completion block.
ResponseJSON Struct:
struct ResponseJSON: Codable {
let network: Stations
}
One way to solve this is to use a closure. To simplify things create a struct that holds the values
struct BikeResponse {
let status: String
let freeBikes: Int
let emptySlots: Int
}
And then modify your function declaration to
func getBikeData(stationName:String, completion: (BikeResponse)->(Void)){
and then after the decoding you call the completion handler
if let station = model.network.stations.first(where: { $0.name == stationName }) {
let response = BikeResponse(status: station.extra.status,
freeBikes: station.free_bikes,
emptySlots: station.empty_slots)
completion(response)
And then in your completion code you can assign the values to your labels
getBikeData(stationName: "ABC") { response in
DispatchQueue.main.async {
someLabel.text = response.status
//...
}
}
Simplest solution:
if let station = model.network.stations.first(where: { $0.name == stationName }) {
DispatchQueue.main.async {
self.emptySlotsLabel.text = String(station.empty_slots)
self.freeBikesLabel.text = String(station.free_bikes)
self.statusLabel.text = station.extra.status
}
}
emptySlotsLabel, freeBikesLabel and statusLabel are the labels, change the names to the real names
you need to add completion handler to you function. Because you are trying to make async query.
After calling getbikedata func you can assign value to your labels.
Sample code looks like this:
func getBikeData(stationName:String, completion: #escaping (Station) -> Void) {
// ... your code here
if let station = model.network?.stations?.first(where: { $0.name == stationName }) {
//get the properties of station here as per requirement
// let emptySlots: Int = station.emptySlots!
// let freeBikes: Int = station.freeBikes!
// let Status: String = (station.extra?.status)!.rawValue
completion(station)
}
// ... other your code here
}
And usage:
getBikeData(stationName: stationName) { (station) in
print(station)
// For example
label.text = station.emptySlots
}

Function runs twice if nested async calls are executed and once otherwise. Need help pre-determining when this will happen

func handleGetAllPhotoURLs is called from the line below and I have confirmed that the line of code only executes once with breakpoints.
_ = FlickrClient.getAllPhotoURLs(currentPin: self.currentPin, fetchCount: fetchCount, completion: self.handleGetAllPhotoURLs(pin:urls:error:))
According to output from my print statements, the function runs twice because it prints two lines of output if urls.count is non-zero. However, if urls.count is zero then I only get one print statement that states "urls.count ---> 0"
handleGetAllPhotoURLs ---> urls.count ---> 0 //this line is always printed
handleGetAllPhotoURLs ---> urls.count ---> 21 //this line is only printed if the urls parameter is not empty
func handleGetAllPhotoURLs(pin: Pin, urls: [URL], error: Error?){
print("handleGetAllPhotoURLs ---> urls.count ---> \(urls.count)")
let backgroundContext: NSManagedObjectContext! = dataController.backGroundContext
if let error = error {
print("func mapView(_ mapView: MKMapView, didSelect... \n\(error)")
return
}
let pinId = pin.objectID
backgroundContext.perform {
let backgroundPin = backgroundContext.object(with: pinId) as! Pin
backgroundPin.urlCount = Int32(urls.count)
try? backgroundContext.save()
}
for (index, currentURL) in urls.enumerated() {
URLSession.shared.dataTask(with: currentURL, completionHandler: { (imageData, response, error) in
guard let imageData = imageData else {return}
connectPhotoAndPin(dataController: self.dataController, currentPin: pin , data: imageData, urlString: currentURL.absoluteString, index: index)
}).resume()
}
}
In addition, I have a UILabel that only reveals itself when urls.count is zero and I only want to reveal it when urls is empty.
Right now, if urls is not empty, the app is very quickly flashing the empty message UILabel. Which now makes sense to me because print statement shows that urls array is temporarily empty.
Is there a way for me to determine to avoid flashing the empty message UILabel to user when urls.count is non-zero?
edit: Added code below based on request. The function below is called to obtain [URL] in completion handler. Then the completion handler is fed into:
func handleGetAllPhotoURLs(pin: Pin, urls: [URL], error: Error?)
class func getAllPhotoURLs(currentPin: Pin, fetchCount count: Int, completion: #escaping (Pin, [URL], Error?)->Void)-> URLSessionTask?{
let latitude = currentPin.latitude
let longitude = currentPin.longitude
let pageNumber = currentPin.pageNumber
let url = Endpoints.photosSearch(latitude, longitude, count, pageNumber).url
var array_photo_URLs = [URL]()
var array_photoID_secret = [[String: String]]()
var array_URLString = [String]()
var array_URLString2 = [String]()
var count = 0
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let dataObject = data, error == nil else {
DispatchQueue.main.async {
completion(currentPin, [], error)
}
return
}
do {
let temp = try JSONDecoder().decode(PhotosSearch.self, from: dataObject)
temp.photos.photo.forEach{
let tempDict = [$0.id : $0.secret]
array_photoID_secret.append(tempDict)
let photoURL = FlickrClient.Endpoints.getOnePicture($0.id, $0.secret)
let photoURLString = photoURL.toString
array_URLString.append(photoURLString)
getPhotoURL(photoID: $0.id, secret: $0.secret, completion: { (urlString, error) in
guard let urlString = urlString else {return}
array_URLString2.append(urlString)
array_photo_URLs.append(URL(string: urlString)!)
count = count + 1
if count == temp.photos.photo.count {
completion(currentPin, array_photo_URLs, nil)
}
})
}
completion(currentPin, [], nil)
return
} catch let conversionErr {
DispatchQueue.main.async {
completion(currentPin, [], conversionErr)
}
return
}
}
task.resume()
return task
}
In the do block, you are calling completion twice. Please see the correction,
do {
let temp = try JSONDecoder().decode(PhotosSearch.self, from: dataObject)
if temp.photos.photo.isEmpty == false {
temp.photos.photo.forEach{
let tempDict = [$0.id : $0.secret]
array_photoID_secret.append(tempDict)
let photoURL = FlickrClient.Endpoints.getOnePicture($0.id, $0.secret)
let photoURLString = photoURL.toString
array_URLString.append(photoURLString)
getPhotoURL(photoID: $0.id, secret: $0.secret, completion: { (urlString, error) in
guard let urlString = urlString else {return}
array_URLString2.append(urlString)
array_photo_URLs.append(URL(string: urlString)!)
count = count + 1
if count == temp.photos.photo.count {
completion(currentPin, array_photo_URLs, nil)
}
})
}
} else {
completion(currentPin, [], nil)
}
return
}

How to wait for Swift's URLSession to finish before running again?

Probably a stupid question, but I'm a beginner at this.
The below code is supposed to get book information from Google Books from a keyword search. It then goes through the results and checks if I have a matching ISBN in a Firebase database. It works, but currently can only search 40 books as that's the Google Books API maximum per search.
Fortunately, I can specify where to start the index and get the next 40 books to search as well. Unfortunately, I've been trying for hours to understand how the URLSession works. All the methods I've tried have shown me that the code after the URLSession block doesn't necessarily wait for the session to complete. So if I check if I've found any matches afterward, it might not even be done searching.
I suspect the answer is in completion handling, but my attempts so far have been unsuccessful. Below is my code with a URL setup to take various starting index values.
var startingIndex = 0
//encode keyword(s) to be appended to URL
let query = query.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url = "https://www.googleapis.com/books/v1/volumes?q=\(query)&&maxResults=40&startIndex=\(startingIndex)"
URLSession.shared.dataTask(with: URL(string: url)!) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}else{
let json = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
if let items = json["items"] as? [[String: AnyObject]] {
//for each result make a book and add title
for item in items {
if let volumeInfo = item["volumeInfo"] as? [String: AnyObject] {
let book = Book()
//default values
book.isbn13 = "isbn13"
book.isbn10 = "isbn10"
book.title = volumeInfo["title"] as? String
//putting all authors into one string
if let temp = volumeInfo["authors"] as? [String] {
var authors = ""
for i in 0..<temp.count {
authors = authors + temp[i]
}
book.author = authors
}
if let imageLinks = volumeInfo["imageLinks"] as? [String: String] {
book.imageURL = imageLinks["thumbnail"]
}
//assign isbns
if let isbns = volumeInfo["industryIdentifiers"] as? [[String: String]] {
for i in 0..<isbns.count {
let firstIsbn = isbns[i]
if firstIsbn["type"] == "ISBN_10" {
book.isbn10 = firstIsbn["identifier"]
}else{
book.isbn13 = firstIsbn["identifier"]
}
}
}
//adding book to an array of books
myDatabase.child("listings").child(book.isbn13!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
if listings.contains(book) == false{
listings.append(book)
}
DispatchQueue.main.async { self.tableView.reloadData() }
}
})
myDatabase.child("listings").child(book.isbn10!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
if listings.contains(book) == false{
listings.append(book)
}
DispatchQueue.main.async { self.tableView.reloadData() }
}
})
}
}
}
}
SVProgressHUD.dismiss()
}.resume()
Below is my revised code:
func searchForSale(query: String, startingIndex: Int) {
directionsTextLabel.isHidden = true
tableView.isHidden = false
listings.removeAll()
DispatchQueue.main.async { self.tableView.reloadData() }
SVProgressHUD.show(withStatus: "Searching")
//clear previous caches of textbook images
cache.clearMemoryCache()
cache.clearDiskCache()
cache.cleanExpiredDiskCache()
let url = "https://www.googleapis.com/books/v1/volumes?q=\(query)&&maxResults=40&startIndex=\(startingIndex)"
URLSession.shared.dataTask(with: URL(string: url)!) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}else{
var needToContinueSearch = true
let json = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
if json["error"] == nil {
let totalItems = json["totalItems"] as? Int
if totalItems == 0 {
SVProgressHUD.showError(withStatus: "No matches found")
return
}
if let items = json["items"] as? [[String: AnyObject]] {
//for each result make a book and add title
for item in items {
if let volumeInfo = item["volumeInfo"] as? [String: AnyObject] {
let book = Book()
//default values
book.isbn13 = "isbn13"
book.isbn10 = "isbn10"
book.title = volumeInfo["title"] as? String
//putting all authors into one string
if let temp = volumeInfo["authors"] as? [String] {
var authors = ""
for i in 0..<temp.count {
authors = authors + temp[i]
}
book.author = authors
}
if let imageLinks = volumeInfo["imageLinks"] as? [String: String] {
book.imageURL = imageLinks["thumbnail"]
}
//assign isbns
if let isbns = volumeInfo["industryIdentifiers"] as? [[String: String]] {
for i in 0..<isbns.count {
let firstIsbn = isbns[i]
//checks if isbns have invalid characters
let isImproperlyFormatted = firstIsbn["identifier"]!.contains {".$#[]/".contains($0)}
if isImproperlyFormatted == false {
if firstIsbn["type"] == "ISBN_10" {
book.isbn10 = firstIsbn["identifier"]
}else{
book.isbn13 = firstIsbn["identifier"]
}
}
}
}
//adding book to an array of books
myDatabase.child("listings").child(book.isbn13!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
if listings.contains(book) == false{
listings.append(book)
needToContinueSearch = false
}
DispatchQueue.main.async { self.tableView.reloadData() }
}
})
myDatabase.child("listings").child(book.isbn10!).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists() {
if listings.contains(book) == false{
listings.append(book)
needToContinueSearch = false
}
DispatchQueue.main.async { self.tableView.reloadData() }
return
}
if startingIndex < 500 {
if needToContinueSearch {
let nextIndex = startingIndex + 40
self.searchForSale(query: query, startingIndex: nextIndex)
}
}
})
}
}
}
}else{
return
}
}
SVProgressHUD.dismiss()
}.resume()
//hide keyboard
self.searchBar.endEditing(true)
}
In your completion handler if any results have been returned you end with:
DispatchQueue.main.async { self.tableView.reloadData() }
to trigger reloading of your table with the updated information. At this same point is where you could determine of there may be more results and initiate the next asynchronous URL task. In outline your code might be:
let needToContinueSearch : Bool = ...;
DispatchQueue.main.async { self.tableView.reloadData() }
if needToContinueSearch
{ // call routine it initiate next async URL task
}
(If there is any reason to start the task from the main thread the if would be in the block.)
By not initiating the next search until after you've processed the results of the first you avoid having to deal with any issues of a subsequent callback trying to update your data at the same time as a previous one.
However if you find delaying the second search in this way is too slow you can investigate ways to overlap the operations, e.g. you might have the callback just pass the processing of the results to an async task on a serial queue (so that only one set of results is being processed at once) and initiate the next async URL task.
HTH
Declare a bool variable as isLoading and if that function is loading dont trigger urlsession. hope below sample will help you.
var isLoading : Bool = false
func loadMore(with pageCount: Int){
if isLoading { return }
isLoading = true
// call the network
URLSession.shared.dataTask(with: URL(string: "xxxxx")!) { (data, response, error) in
// after updating the data set isloding to false again
// do the api logic here
//
DispatchQueue.main.async {
// self.items = downloadedItems
self.tableView.reloadData()
self.isLoading = false
}
}.resume()
}

Mulitple calls to meetup api swift

I am trying to get a list of events from the meetup's api. Its crashes when request mulitple times as it states can only request once and is limited to 200 requests. I am getting groups then using the "urlName" api call to get the event coming up for that group. The result would be an array of events from a bunch of meetup groups. Here is my code.
func getEventsFromMeetup(complete: (groups: [Meetup], succes: Bool) -> Void) {
var currentUserInterests = [String]()
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let recommendedBaseUrl = "https://api.meetup.com/recommended/groups?key=\(meetupAPIKey)"
let url = NSURL(string: recommendedBaseUrl)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
session.dataTaskWithRequest(request) { (data, response, error) in
guard error == nil else {
print(error)
complete(groups: [Meetup](), succes: false)
return
}
guard let data = data else {
print("Error with data")
complete(groups: [Meetup](), succes: false)
return
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as! [NSDictionary]
var groups = [Meetup]()
CurrentUserFM.sharedInstance.getCurrentUserInterests { (interests) in
currentUserInterests = interests
var ints = [String]()
print(json.count)
for j in json {
let m = Meetup(data: j)
ints.append(m.name!)
if let i = m.interestName {
if currentUserInterests.contains(i) {
groups.append(m)
}
}
print("ints: \(ints.count)")
print("json: \(json.count)")
if Int(ints.count) >= Int(json.count) {
dispatch_async(dispatch_get_main_queue(), {
complete(groups: groups, succes: true)
return
})
}
}
}
}catch {
print(error)
}
}.resume()
}
func getEventsForGroups(completionHandler: (meetupEvents: [MeetupEvent]) -> ()) {
self.getEventsFromMeetup { (groups, success) in
var meetupEvents1 = [MeetupEvent]()
var ints = [String]()
for group in groups {
let eventBaseUrl = "https://api.meetup.com/\(group.urlname!)?key=\(meetupAPIKey)"
let url = NSURL(string: eventBaseUrl)
let request = NSMutableURLRequest(URL: url!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
session.dataTaskWithRequest(request, completionHandler: { (data, reponse, error) in
guard error == nil else {
print(error)
return
}
guard let data = data else {
print("Error with data")
return
}
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
let m = MeetupEvent()
if let name = json["name"] as? String {
m.name = name
}
if let link = json["link"] as? String {
m.link = link
}
if let interestName = group.name {
m.interestName = interestName
}
if let image = json["key_photo"]??["thumb_link"] as? String {
m.image = image
}
meetupEvents1.append(m)
ints.append("1")
if Int(ints.count) >= Int(groups.count) {
dispatch_async(dispatch_get_main_queue(), {
completionHandler(meetupEvents: meetupEvents1)
return
})
}
}catch {
print(error)
}
}).resume()
}
}
}
Any suggestions on how to get the events from the groups so that the app wont crash from meetup calls?

Extra argument 'error' in call in swift

I am new to swift so please treat me as beginner.
I am following tutorial, this is pretty old tutorial and it has used GoogleMap framework whereas I am doing it with pod. In func geocodeAddress in MapTasks.swift file I am getting error called
Extra argument 'error' in call
func geocodeAddress(address: String!, withCompletionHandler completionHandler: ((status: String, success: Bool) -> Void)) {
if let lookupAddress = address {
var geocodeURLString = baseURLGeocode + "address=" + lookupAddress
geocodeURLString = geocodeURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let geocodeURL = NSURL(string: geocodeURLString)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let geocodingResultsData = NSData(contentsOfURL: geocodeURL!)
let request = NSMutableURLRequest(URL: geocodingResultsData)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(let data, let response, let error) in
if let _ = response as? NSHTTPURLResponse {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
if error != nil {
print("error=\(error!)")
return
}
if let parseJSON = json {
}
} catch {
print(error)
}
}
}
task.resume()
else {
// Get the response status.
let status = dictionary["status"] as! String
if status == "OK" {
let allResults = dictionary["results"] as! Array<Dictionary<NSObject, AnyObject>>
self.lookupAddressResults = allResults[0]
// Keep the most important values.
self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as! String
let geometry = self.lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject>
self.fetchedAddressLongitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue
self.fetchedAddressLatitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue
completionHandler(status: status, success: true)
}
else {
completionHandler(status: status, success: false)
}
}
})
}
else {
completionHandler(status: "No valid address.", success: false)
}
}
So far I know is I am getting this error because of the diffrent version of swift. Tutorial I am following is written in old version of swift and I am doing it in new
In Swift 2.0, you cannot add 'error' argument in NSJSONSerialization method, you need to use try-catch statement as follows:
func geocodeAddress(address: String!, withCompletionHandler completionHandler: ((status: String, success: Bool) -> Void)) {
if let lookupAddress = address {
var geocodeURLString = baseURLGeocode + "address=" + lookupAddress
geocodeURLString = geocodeURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let geocodeURL = NSURL(string: geocodeURLString)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let geocodingResultsData = NSData(contentsOfURL: geocodeURL!)
let request = NSMutableURLRequest(URL: geocodeURL!)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(let data, let response, let error) in
if let _ = response as? NSHTTPURLResponse {
do {
let dictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
if error != nil {
print("error=\(error!)")
return
}
if let parseJSON = dictionary {
let status = dictionary["status"] as! String
if status == "OK" {
let allResults = dictionary["results"] as! Array<Dictionary<NSObject, AnyObject>>
self.lookupAddressResults = allResults[0]
// Keep the most important values.
self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as! String
let geometry = self.lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject>
self.fetchedAddressLongitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue
self.fetchedAddressLatitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue
completionHandler(status: status, success: true)
}
else {
completionHandler(status: status, success: false)
}
}
} catch {
print(error)
}
}
}
task.resume()
})
}
}