Swift 4 “Extra argument in call” Rxswift

2019-07-07 02:50发布

问题:

I updated from Swift 3 to 4, and i am using RxSwift, When updated i came across an error "Extra argument in call" as it expects now of type element. I tried creating tuple of (response, data) but still gives me an error as shown.

     public static func deleteCrush(receiver: String, receiver_type: ProviderType) -> Observable<(HTTPURLResponse, NSArray)> {
    /*
     Delete crush identified by `id`

   */

    let parameters: Parameters = [
        "receiver": receiver,
        "receiver_type": receiver_type.rawValue
    ]

    print("deleteCrush paramreters: \(parameters)")

    return Observable.create({ (observer) -> Disposable in
        Alamofire.request(Router.deleteCrush(parameters: parameters))
            .rx
            .responseJSON()
            .debug()
            .subscribe(onNext: { (response, json) in
                print("deleteCrush response code: ", response.statusCode)
                if let data = json as? NSArray {
                    observer.on(.next(response, data))
                    observer.on(.completed)
                }
            }, onError: { (error) in
                print("deleteCrush error: ", error)
                observer.on(.error(error))
            }, onCompleted: nil, onDisposed: nil)
    })
}

Error: Extra argument in call.
After trying to fix:

     var tuple = (response, json)
                 if let data = json as? NSArray {
                    observer.on(.next(tuple))
                    observer.on(.completed)
                }

Error:

Event<(HTTPURLResponse, NSDictionary)>' produces result of type 'Event', but context expects 'Event<(HTTPURLResponse, NSDictionary)>

回答1:

It just seems like you are not passing correct data type to your events.

Either simply wrap your tuple properly,

observer.on(.next((response, data)))

Or then use proper tuple type for your Element type,

if let data = json as? NSArray {
   var tuple = (response, data)

   observer.on(.next(tuple))
   observer.on(.completed)
 }

Note that you are setting tuple as tuple = (response, json) which is not correct according to your method return type.



回答2:

Swift is probably having a hard time figuring out the type of the of the data you are sending in .next. Sometimes this happens and the compiler provides a completely useless error message

Check your function definition. Specifically the return type. The observer you're returning may not be of the same type you are sending to your observers.