Nil 与预期的参数类型不兼容 'JSON'

Nil is not compatible with expected argument type 'JSON'

以前它在 Swift 3 上运行良好,但是当我切换到 Swift 4 时,无法找到类型 JSON(SwiftyJson Library)

的完成问题
static func getRoster(information: [String: String], completion:@escaping ([Player],JSON,Paging?,WSResponse,NSError?) -> Void){
        Alamofire.request(NetWorkingConstants.baseURL+NetWorkingConstants.Team.get_all_roster, method: .post, parameters: information, encoding: JSONEncoding.default, headers:StarsLeagueUser.sharedInstance.getDefaultHeaders()).responseJSON { (response) in
            switch response.result {
            case .success(let value):
                let json = JSON(value)
                let wsResponse = WSResponse(code: json["response_code"].stringValue, message: json["message"].stringValue)


                if wsResponse.code == ServerCode.success{
                    print("Success")

                }else if wsResponse.code == ServerCode.unauthorized{
                    print("Session Expire")
                }
            case .failure(let error):

               print("Network Error")
                let wsResponse = WSResponse(code: "1000", message: "Network Error")
                completion([],nil,nil,wsResponse,error as NSError?)//Here is error - Nil is not compatible with expected argument type 'JSON'
                print("GetRoster")
            }
        }
    }

更改为 ( ,JSON? )

func getRoster(information: [String: String], 
 completion:@escaping ([Player],JSON?,Paging?,WSResponse,NSError?) -> Void){

至于能够return一个零值,那么它应该是可选的

将 return 类型 JSON 设为非可选意味着您必须 return 一个非可选值,因此 returning nil 是一个像您一样的问题做

var str1:String = nil // error 
var str2:String? = nil // ok  

错误是因为如果参数类型是非可选的,你不能传递nil。将 JSON 声明为可选 (JSON?)。

在您的情况下,我建议使用具有关联类型的枚举作为结果类型。

好处是参数安排得更好,而且你总是有非可选类型,因为你 return 只使用相关参数

enum Result {
   case success([Player], JSON, Paging, WSResponse)
   case failure(WSResponse, Error)
}

然后声明你的方法

func getRoster(information: [String: String], completion:@escaping (Result) -> Void) {

和return在失败案例中

let wsResponse = WSResponse(code: "1000", message: "Network Error")
completion(.failure(wsResponse, error))

在调用方法中使用 switch

switch result {
    case let .success(players, json, paging, response):
    // handle success
    case let .failure(response, error):
    // handle error
}