I’m getting the following error message: “Missing argument for parameter ‘hours’ in call
, where “hours
” is an attribute of a struct, and I’m getting this error message in the line of code: “var selectedVenue = SelectedRestaurantDetailViewInfoNotUsingCodable()
” in the code snippet further below.
There is a “Fix” button for this error message that says to add in the argument, and have the parameter be assigned to a different struct’s name which is the data type of “hours
” in the first struct mentioned. The text just to the left of this “Fix” button: “Insert ', hours: <#OpenHoursForDaysOfWeek#>’
”.
I’ve tried this “Fix” button, except I took out the extra comma and space before "hours
" in the suggested line of code from the "Fix" button, also the result was the same as when the space and comma I took out were there, as I tried both. In this line of code where the "Fix" button is used, the assignment for the parameter of “hours
” is set to the data type that the struct attribute also called “hours
” is set to in the file with these structs, however I get the error message: “Cannot convert value of type 'OpenHoursForDaysOfWeek.Type' to expected argument type ‘OpenHoursForDaysOfWeek’
" when I do this, where “OpenHoursForDaysOfWeek
” is the data type that the “hours
” attribute is set to, and which is a struct itself.
I’ve included the file that I’m receiving this “Missing argument for parameter ‘hours’ in call
” in the below code snippet, and the file that has both of these struct definitions, where one struct has the “hours
” attribute, and the other struct is the struct that is the data type for the just mentioned “hours
” attribute.
Code:
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
}
Thanks!
You have to set the var hours: OpenHoursForDaysOfWeek?
Optional.
Or if is not Optional, you have to declare the value when you instantiate it
var selectedVenue = SelectedRestaurantDetailViewInfoNotUsingCodable(hours: OpenHoursForDaysOfWeek(mondayOpenHoursWithoutDay: "text",
tuesdayOpenHoursWithoutDay: "text",
wednesdayOpenHoursWithoutDay: "text",
thursdayOpenHoursWithoutDay: "text",
fridayOpenHoursWithoutDay: <"text",
saturdayOpenHoursWithoutDay: "text",
sundayOpenHoursWithoutDay: "text",
numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse: "text"))