iDeveloper
iDeveloper

Reputation: 2444

Nil is not compatible with expected argument type 'JSON'

Previously it's working well with Swift 3, But when I switch to Swift 4, Unable to find issue with completion with type 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")
            }
        }
    }

Upvotes: 0

Views: 3838

Answers (2)

vadian
vadian

Reputation: 285082

The error occurs because you cannot pass nil if the parameter type is non-optional. Declare JSON as optional (JSON?).

In your case I recommend to use an enum with associated types as result type.

The benefit is the parameters are better arranged and you have always non-optional types because you are returning only the relevant parameters

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

Then declare your method

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

and return in the failure case

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

In the caller method use a switch

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

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

Change to ( ,JSON? )

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

As to be able to return a nil value , then it should be optional

Making the return type JSON as non-optional means that you have to return a non - optional value so returning nil is a problem like you do

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

Upvotes: 3

Related Questions