Update: The code for the function retrieveSelectedRestaurantDetailViewInfo
is posted further below in the middle of the post.
I’m getting the error message: “Extra trailing closure passed in call
” in the below code block:
retrieveSelectedRestaurantDetailViewInfo(selected_restaurant_business_ID: theMapVenue.id!) { (response, error) in
if let response = response {
//Got error in below line of code. May not need to use below line of code. Try and figure out correct code for below line of code, if adding code inthere similar to commented out below line of code makes program/project better.
self.selectedVenueDetailViewInfo = response
DispatchQueue.main.async {
self.scrollableCitiesRestaurantDetailsTableView.reloadData()
}
}
}
I’m getting the error message in the line of code (in the above code block):
retrieveSelectedRestaurantDetailViewInfo(selected_restaurant_business_ID: theMapVenue.id!) { (response, error) in
The code for the retrieveSelectedRestaurantDetailViewInfo
function is below.
RetrieveSelectedRestaurantDetailViewInfo.swift
:
import Foundation
import UIKit
import CoreLocation
extension UIViewController {
func retrieveSelectedRestaurantDetailViewInfo(
selected_restaurant_business_ID: String) async throws -> SelectedRestaurantDetailViewInfoNotUsingCodable? {
// MARK: Make API Call
let apiKey = ApiKey
let baseURL =
"https://api.yelp.com/v3/businesses/\(selected_restaurant_business_ID)"
let url = URL(string: baseURL)
/// Creating Request
var request = URLRequest(url: url!)
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.httpMethod = "GET"
let (data, response) = try await URLSession.shared.data(for: request)
let json = try JSONSerialization.jsonObject(with: data, options: [])
let responseDictionary = json as? NSDictionary
var selectedVenue = SelectedRestaurantDetailViewInfoNotUsingCodable(hours: OpenHoursForDaysOfWeek(mondayOpenHoursWithoutDay: "Starting Text",
tuesdayOpenHoursWithoutDay: "Starting Text",
wednesdayOpenHoursWithoutDay: "Starting Text",
thursdayOpenHoursWithoutDay: "Starting Text",
fridayOpenHoursWithoutDay: "Starting Text",
saturdayOpenHoursWithoutDay: "Starting Text",
sundayOpenHoursWithoutDay: "Starting Text",
numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse: "Starting Text"))
//Accessing Business Data
selectedVenue.name = responseDictionary?.value(forKey: "name") as? String
//*Code to account for if selectedVenue.name is nil, or if it doesn't have any text, specfically has/is "", or if it only has a space specfically like this: " ".*
//*Code for accessing the rest of the detail info for the user-selected restaurant (address, business hours, etc.), is similar to the above code for accessing the business name above, and also accounts if the other info accessed is nil, or if it doesn't have any text, specfically has/is "", or if it only has a space specfically like this: " ".*
//Code for accessing business hours info. Included it here since its a little different than accessing the rest of the business information above.
if let hoursDictionariesForHoursManipulation = responseDictionary?.value(forKey: "hours") as? [NSDictionary] {
selectedVenue.hours = self.manipulateSelectedRestaurantHoursInfoAndRecieveFormattedHoursInfoToUse(selected_restaurant_business_hours: hoursDictionariesForHoursManipulation)
}
return selectedVenue
}
}
One thing I’ve tried: Seeing if model’s properties of whom’s model was used in the code block were set to var/set appropriately. Idea came from this page: https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-88-extra-trailing-closure-passed-in-call/10731.
Things I think may be causing this error message:
-There is no variable created for error
, as there were similar solutions to this same type of error message that I found on Stack Overflow here: Error=Extra trailing closure passed in call,.
-This error message may be being caused by whatever is causing another error message in the same code block above, with this error message being: “Cannot assign value of type '_' to type ‘SelectedRestaurantDetailViewInfoNotUsingCodable’
”, in the line of code:
self.selectedVenueDetailViewInfo = response
The SelectedRestaurantDetailViewInfoNotUsingCodable
is assigned to selectedVenueDetailViewInfo
further “above” in the same code file of the code snippets posted above, which isn’t shown here.
I haven’t found any potential solutions for this second error message yet.
How do I solve at least the first error message mentioned in this post?
As you've posted, the signature of retrieveSelectedRestaurantDetailViewInfo
is:
func retrieveSelectedRestaurantDetailViewInfo(selected_restaurant_business_ID: String) async throws -> SelectedRestaurantDetailViewInfoNotUsingCodable?
There is no completion handler. It uses async
and it has a direct return value.
So the calling code must use await
(and try
due to the throws
):
do {
let response = try await retrieveSelectedRestaurantDetailViewInfo(selected_restaurant_business_ID: theMapVenue.id!)
self.selectedVenueDetailViewInfo = response
self.scrollableCitiesRestaurantDetailsTableView.reloadData()
} catch {
print(error)
}
Note how this is very similar to your use of URLSession.data
in the line:
let (data, response) = try await URLSession.shared.data(for: request)