为什么我得到这个快速错误?在调用中缺少参数'price'的参数

时间:2021-09-04 22:46:17

Issue: I'm trying to create a custom object with json data, however swift is giving me an error thats theres a missing parameter when im looping/adding data to the object, Yet in the model im using an optional so I'm confused where the problem lies. ultimately this data will go into a table view.

问题:我正在尝试使用json数据创建一个自定义对象,但是当我循环/向对象添加数据时,swift正在给我一个错误,这是一个缺少的参数,但是在模型中我使用了一个可选项,所以我很困惑问题所在。最终这些数据将进入表格视图。

My model (using optionals):

我的模型(使用选项):

class FlightDataModel {
    var airline: String?
    var price: String?

    init(airline: String?, price: String?) {
        self.airline = airline
        self.price = price
    }  
}

The alamofire API request..

alamofire API请求..

 request(qpxRequest).responseJSON { (request, response, json, error) -> Void in
        if response != nil {
            //NSLog("%@", response!)
        }

        if json != nil {
            if let myJSON = json as? [String:AnyObject] {
                if let trips = myJSON["trips"] as? [String:AnyObject] {
                    if let data = trips["data"] as? [String:AnyObject] {
                        if let carriers = data["carrier"] as? [[String:String]] {

                            for (index, carrierName) in enumerate(carriers) {

// -----PROBLEM AREA-------------

// -----问题区域-------------

                             let myFlight = FlightDataModel(airline: carrierName["name"] as String)

                                self.arrayOfFlights[index] = myFlight
                                //println("\(self.arrayOfFlights[index].airline)")


                            }
                        }
                    }
                }
            }

1 个解决方案

#1


price is a parameter to your init constructor. An argument must be provided, even if the parameter is of an optional type. If you don't have a value for it, you can give it nil:

price是init构造函数的参数。必须提供参数,即使参数是可选类型也是如此。如果你没有它的价值,你可以给它零:

let myFlight = FlightDataModel(airline: carrierName["name"] as String, price: nil)

If you wanted price to default to nil if that argument is not provided you do one of two things:

如果您希望价格默认为nil,如果未提供该参数,则执行以下两项操作之一:

  1. You could provide a default value for price in the initializer:

    您可以在初始化程序中提供价格的默认值:

    init(airline: String?, price: String? = nil) {
        self.airline = airline
        self.price = price
    }
    
  2. You could provide a second separate init that just takes an airline:

    您可以提供第二个单独的初始化,只需要一个航空公司:

    init(airline: String?) {
        self.airline = airline
    }
    

#1


price is a parameter to your init constructor. An argument must be provided, even if the parameter is of an optional type. If you don't have a value for it, you can give it nil:

price是init构造函数的参数。必须提供参数,即使参数是可选类型也是如此。如果你没有它的价值,你可以给它零:

let myFlight = FlightDataModel(airline: carrierName["name"] as String, price: nil)

If you wanted price to default to nil if that argument is not provided you do one of two things:

如果您希望价格默认为nil,如果未提供该参数,则执行以下两项操作之一:

  1. You could provide a default value for price in the initializer:

    您可以在初始化程序中提供价格的默认值:

    init(airline: String?, price: String? = nil) {
        self.airline = airline
        self.price = price
    }
    
  2. You could provide a second separate init that just takes an airline:

    您可以提供第二个单独的初始化,只需要一个航空公司:

    init(airline: String?) {
        self.airline = airline
    }