提问人:cg1000 提问时间:5/6/2023 最后编辑:cg1000 更新时间:5/7/2023 访问量:289
收到错误消息:“Extra trailing closure passed in call” 斯威夫特
Getting error message: “Extra trailing closure passed in call” Swift
问:
更新:该函数的代码发布在帖子中间的下方。retrieveSelectedRestaurantDetailViewInfo
我在下面的代码块中收到错误消息:“”:Extra trailing closure passed in call
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()
}
}
}
我在代码行中收到错误消息(在上面的代码块中):
retrieveSelectedRestaurantDetailViewInfo(selected_restaurant_business_ID: theMapVenue.id!) { (response, error) in
该函数的代码如下。retrieveSelectedRestaurantDetailViewInfo
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
}
}
我尝试过的一件事是:查看代码块中使用谁的模型的模型属性是否适当地设置为 var/set。想法来自这个页面:https://www.hackingwithswift.com/forums/100-days-of-swiftui/day-88-extra-trailing-closure-passed-in-call/10731。
我认为可能导致此错误消息的事情:
-没有为 创建变量,因为我在 Stack Overflow 上找到的相同类型的错误消息有类似的解决方案:Error=Extra trailing closure passed in call,。error
- 此错误消息可能是由上述同一代码块中导致另一条错误消息的原因引起的,此错误消息为:“”,在代码行中:Cannot assign value of type '_' to type ‘SelectedRestaurantDetailViewInfoNotUsingCodable’
self.selectedVenueDetailViewInfo = response
在上面发布的代码片段的同一代码文件中,它被分配给进一步的“上面”,此处未显示。SelectedRestaurantDetailViewInfoNotUsingCodable
selectedVenueDetailViewInfo
我还没有找到第二个错误消息的任何潜在解决方案。
如何解决本文中提到的至少第一个错误消息?
答:
正如你所发布的,签名是:retrieveSelectedRestaurantDetailViewInfo
func retrieveSelectedRestaurantDetailViewInfo(selected_restaurant_business_ID: String) async throws -> SelectedRestaurantDetailViewInfoNotUsingCodable?
没有完成处理程序。它使用并具有直接返回值。async
因此,调用代码必须使用(并且由于):await
try
throws
do {
let response = try await retrieveSelectedRestaurantDetailViewInfo(selected_restaurant_business_ID: theMapVenue.id!)
self.selectedVenueDetailViewInfo = response
self.scrollableCitiesRestaurantDetailsTableView.reloadData()
} catch {
print(error)
}
请注意,这与您在行中的用法非常相似:URLSession.data
let (data, response) = try await URLSession.shared.data(for: request)
评论
retrieveSelectedRestaurantDetailViewInfo
id:
restaurantId:
selected_restaurant_business_ID
async
async
retrieveSelectedRestaurantDetailViewInfo
try
await
retrieveSelectedRestaurantDetailViewInfo
async-await