user2636197 user2636197 - 25 days ago 7
iOS Question

Swift result of call to is unused

I cant figure out how to remove this warning in swift3:

Result of call to 'responseMessagesArray' is unused

This is how my code looks like:

fileprivate class func getMessagesAtPath(_ path: String, tokenKey: String, completionHandler: @escaping (MyMessagesWrapper?, NSError?) -> Void) {
let credentialData = ":\(tokenKey)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])

let headers = ["Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Basic \(base64Credentials)"]

Alamofire.request(path, headers: headers)
.responseMessagesArray { response in
if let error = response.result.error
{
completionHandler(nil, error as NSError?)
return
}
completionHandler(response.result.value, nil)
}
}

class func getMyMessages(_ completionHandler: @escaping (MyMessagesWrapper?, NSError?) -> Void) {
getMessagesAtPath(MyMessages.endpointForMyMessages(),tokenKey: MyMessages.getTokenKey(),completionHandler: completionHandler)
}

extension Alamofire.DataRequest {
func responseMessagesArray(_ completionHandler: @escaping (DataResponse<MyMessagesWrapper>) -> Void) -> Self {
let responseSerializer = DataResponseSerializer<MyMessagesWrapper> { request, response, data, error in
guard error == nil else
{
return .failure(error!)
}
guard let responseData = data else {
return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))

}

let JSONResponseSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, responseData, error)

switch result {
case .success(let value):
let json = JSON(value)
let wrapper = MyMessagesWrapper()

wrapper.next = json["messages"]["next_page_url"].stringValue
wrapper.previous = json["messages"]["prev_page_url"].stringValue
wrapper.count = json["messages"]["total"].intValue

var allMessages:Array = Array<MyMessages>()
print(json)
let results = json["messages"]["data"]
//print(results)
for jsonAds in results
{
//print(jsonAds.1)
let mesaggesData = MyMessages(json: jsonAds.1, id: Int(jsonAds.0))
allMessages.append(mesaggesData)
}
wrapper.myMessages = allMessages
return .success(wrapper)
case .failure(let error):
return .failure(error)
}
}

return response(responseSerializer: responseSerializer,
completionHandler: completionHandler)
}
}


I get the warning from the fileprivate class func getMessagesAtPath()
just where .responseMessagesArray { response in appears

I never had this warning in swift 2.

jjs jjs
Answer

Assign the return value of the function to _ to explicitly discard it, like this:

_ = somethingThatReturnsAValueYouDontCareAbout()

Swift 3 warns about unused function return values, so you need to show it that you know what you're doing.