ios 获取错误消息:“Extra trailing closure passed in call”Swift

epggiuax  于 2023-05-08  发布在  iOS
关注(0)|答案(1)|浏览(534)

**更新:**函数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
我认为可能导致此错误消息的原因:

  • 没有为error创建变量,因为我在Stack Overflow上找到了类似的解决方案:Error=Extra trailing closure passed in call,
  • 该错误消息可能是由在上述相同代码块中引起另一错误消息的任何东西引起的,其中该错误消息是:“Cannot assign value of type '_' to type ‘SelectedRestaurantDetailViewInfoNotUsingCodable’”,在代码行中:
self.selectedVenueDetailViewInfo = response

SelectedRestaurantDetailViewInfoNotUsingCodable在上面发布的代码片段的同一代码文件中被分配给selectedVenueDetailViewInfo,这里没有显示。
我还没有找到任何潜在的解决方案,这第二个错误信息。
我如何解决至少在这篇文章中提到的第一个错误消息?

3bygqnnd

3bygqnnd1#

正如你所发布的,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)

相关问题