收到错误消息:“Extra trailing closure passed in call” 斯威夫特

Getting error message: “Extra trailing closure passed in call” Swift

提问人:cg1000 提问时间:5/6/2023 最后编辑:cg1000 更新时间:5/7/2023 访问量:289

问:

更新:该函数的代码发布在帖子中间的下方。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

在上面发布的代码片段的同一代码文件中,它被分配给进一步的“上面”,此处未显示。SelectedRestaurantDetailViewInfoNotUsingCodableselectedVenueDetailViewInfo

我还没有找到第二个错误消息的任何潜在解决方案。

如何解决本文中提到的至少第一个错误消息?

iOS Swift 闭包函数 调用 调度队列

评论

0赞 Paulw11 5/6/2023
此外,按照习惯,Swift 使用驼峰大小写,而不是 _,并且该函数已被调用,因此您可以简单地将 or 用于您的参数retrieveSelectedRestaurantDetailViewInfoid:restaurantId:
0赞 cg1000 5/7/2023
另外@Paulw11,感谢您提供有关使用驼峰大小写的 Swift 的信息,以及您提到的参数名称的其他信息。我会按照你的建议改变这个名字。selected_restaurant_business_ID
0赞 Rob Napier 5/7/2023
这是我今天看到的两次之一,我看到有人采用标记的方法并尝试为其添加尾随闭包,这不是工作方式。显然,这是令人困惑的事情。我很想知道是什么导致你以这种方式编写代码,这样我就可以帮助其他有同样困惑的人。没有任何内容说它需要闭包参数。asyncasyncretrieveSelectedRestaurantDetailViewInfo
1赞 cg1000 5/8/2023
@RobNapier我包含了尾随闭包,因为我忘记使用并且喜欢 HangarRash 在他的回答中描述的下面,因为最初,这个函数没有使用(并且在该版本的函数中,尾随闭包被包括在内)。tryawaitretrieveSelectedRestaurantDetailViewInfoasync-await
0赞 Rob Napier 5/8/2023
谢谢。了解随着代码的发展,会出现哪些令人困惑的情况,这真的很有帮助。

答:

1赞 HangarRash 5/7/2023 #1

正如你所发布的,签名是:retrieveSelectedRestaurantDetailViewInfo

func retrieveSelectedRestaurantDetailViewInfo(selected_restaurant_business_ID: String) async throws -> SelectedRestaurantDetailViewInfoNotUsingCodable?

没有完成处理程序。它使用并具有直接返回值。async

因此,调用代码必须使用(并且由于):awaittrythrows

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)

评论

0赞 cg1000 5/8/2023
谢谢你!这是有道理的。我仍然收到一些错误消息,但正在解决它们。如果我需要更多帮助,会问更多问题。谢谢!