获取错误消息:调用”Swift“中缺少参数”structAttributeName“的自变量

qmelpv7a  于 2023-03-07  发布在  Swift
关注(0)|答案(1)|浏览(125)

我收到以下错误消息:"Missing argument for parameter ‘hours’ in call,其中" hours "是一个结构体的属性,我在代码行中得到了以下错误消息:"var selectedVenue = SelectedRestaurantDetailViewInfoNotUsingCodable()"(在下面的代码片段中)。
此错误消息有一个"修复"按钮,指示在参数中添加,并将参数分配给不同的结构体名称,即第一个提到的结构体中的数据类型"hours"。此"修复"按钮左侧的文本:"Insert ', hours: <#OpenHoursForDaysOfWeek#>’"。
我试过这个"修复"按钮,除了我去掉了"修复"按钮建议的代码行中"hours"之前的额外逗号和空格,结果也和我去掉的空格和逗号在那里时一样,因为我都试过了。在使用"修复"按钮的这行代码中,参数"hours"的赋值被设置为struct属性(也称为"hours")在包含这些struct的文件中设置的数据类型,但我收到错误消息:"Cannot convert value of type 'OpenHoursForDaysOfWeek.Type' to expected argument type ‘OpenHoursForDaysOfWeek’",其中"OpenHoursForDaysOfWeek"是"hours"属性设置为的数据类型,它本身是一个结构体。
我在下面的代码片段中包含了接收到"Missing argument for parameter ‘hours’ in call"的文件,以及包含这两个结构体定义的文件,其中一个结构体具有"hours"属性,另一个结构体是刚才提到的"hours"属性的数据类型。

    • 代码:**

File1-FileImWorkingInThatHasErrorMessage

import Foundation
import UIKit
import CoreLocation

extension UIViewController {

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

            // MARK: Make API Call
            let apiKey = "API key"

            /// Create URL
            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"

            ///Initialize session and task
            //Code for not using completionHandler:
            let (data, response) = try await URLSession.shared.data(for: request)
                
            do {
                /// Read data as JSON
                let json = try JSONSerialization.jsonObject(with: data!, options: [])

                /// Main dictionary
                guard let responseDictionary = json as? NSDictionary else {return}

                //Code for accessing restaraunt detail view info, and assigning it to selectedRestarauntDetailViewInfo view model thats a struct.
                //The line of code below is where I'm getting the error message: “Missing argument for parameter ‘hours’  in call".
                var selectedVenue = SelectedRestaurantDetailViewInfoNotUsingCodable()
                
                
                //Rest of code for accessing restaraunt detail view info, and assigning it to seelctedRestarauntDetailViewInfo view model thats a struct.
                selectedVenue.name = responseDictionary?.value(forKey: "name") as? String
                
                //*Rest of code for getting business info including address, hours, etc..*
                //Only the code the code for accessing the hours info is listed here since it may be related to my error message above of “Missing argument for parameter ‘hours’  in call". The rest of the code for accessing other info is similar/in the same format as the line of code above which is "selectedVenue.name = responseDictionary.value(forKey: "name") as? String".
                if let hoursDictionariesForHoursManipulation = responseDictionary?.value(forKey: "hours") as? [NSDictionary] {
                    selectedVenue.hours = self.manipulateSelectedRestaurantHoursInfoAndRecieveFormattedHoursInfoToUse(selected_restaurant_business_hours: hoursDictionariesForHoursManipulation)
                }

                else {
                    selectedVenue.hours = "Hours of Operation for this business are unavailable at this time."
                }
                
                return selectedVenue
                
            } catch {
                print("Caught error")
            }
    }
}

File2-StructsThatAreUsedInFile1

import Foundation
import CoreLocation

struct SelectedRestaurantDetailViewInfoNotUsingCodable {
    var name: String?
    //Rest of attribute definitions for getting data, such as address, image URLs, etc., and are in the same format as the "name" attribute definition in the line of code above.
    var hours: OpenHoursForDaysOfWeek
}

struct OpenHoursForDaysOfWeek {
    var mondayOpenHoursWithoutDay: String
    var tuesdayOpenHoursWithoutDay: String
    var wednesdayOpenHoursWithoutDay: String
    var thursdayOpenHoursWithoutDay: String
    var fridayOpenHoursWithoutDay: String
    var saturdayOpenHoursWithoutDay: String
    var sundayOpenHoursWithoutDay: String
    var numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse: String
}

谢谢!

aelbi1ox

aelbi1ox1#

您必须将var hours: OpenHoursForDaysOfWeek?设置为可选。
或者,如果不是Optional,则必须在示例化该值时声明它

var selectedVenue = SelectedRestaurantDetailViewInfoNotUsingCodable(hours: OpenHoursForDaysOfWeek(mondayOpenHoursWithoutDay: "text", 
tuesdayOpenHoursWithoutDay: "text", 
wednesdayOpenHoursWithoutDay: "text", 
thursdayOpenHoursWithoutDay: "text", 
fridayOpenHoursWithoutDay: <"text", 
saturdayOpenHoursWithoutDay: "text", 
sundayOpenHoursWithoutDay: "text", 
numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse: "text"))

相关问题