Search code examples
swiftreactive-swift

ReactiveSwift SignalProducer : Argument 'failed' must precede argument 'value'


I am trying to update an upgrade an old piece of code using ReactSwift because I want ti run it with XCode 9. I have defined a class Network

final class Network: NSObject {
    static func request(_ URLRequest: URLRequestConvertible) -> SignalProducer<Any, NetworkError> {
        return SignalProducer { sink, disposable in
            Alamofire.request(URLRequest)
                .validate()
                .responseJSON { response in
                    switch response.result {
                    case .success(let value):
                        sink.send(value: value)
                        sink.sendCompleted()
                    case .failure(let error):
                        // modification en as NSError
                        sink.send(error: NetworkError(error: error as NSError))
                    }
                }
        }
    }
}

that is called in function using generic type:

private func fetch<T: Himotoki.Decodable>(URLRequest: URLRequestConvertible) -> SignalProducer<T, NetworkError> {
return Network.request(URLRequest)
    .attemptMap { JSON in
        do {
            return .success(try decodeValue(JSON) as T)
        } catch let error {
            return .failure(.incorrectDataReturned)
        }
}

}

The generic function is used to make network requests using Alamofire, like the following one.

// Get static UserData
static func fetchUserData() -> SignalProducer<UserData, NetworkError> {
    return fetch(URLRequest: MoneyCupUsersBackEndRouter.getUserData)
}

So, I call :

    API.fetchUserData()
        .on(started: {
            SVProgressHUD.show()
        },
            value: { cfg in
                globalConfig = cfg
            },
            failed: { [weak self] error in
                self?.view.window?.dodo.error("Impossible de récupérer la configuration globale")
            },
            terminated: {
                SVProgressHUD.dismiss()
        })
        .start()

}

This scheme has worked well before and I have a large number of it across my code. But since I have upgraded to ReactiveCocoa, my code no longer compile. And I have a message stating that :

Argument 'failed' must precede argument 'value'


Solution

  • It used to be that for Swift functions where the arguments have default values you could reorder the arguments as desired at the call site. This feature was removed in Swift 3, which means you now must specify arguments in the same order as they are defined. So for the call to the on function, you must move the value: parameter to the end.