我无法弄清楚如何在swift3中删除此警告:
调用'responseMessagesArray'的结果未使用
这就是我的代码的样子:
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)
}
}
我收到 fileprivate类func getMessagesAtPath()的警告 就在 .responseMessagesArray {response in 出现
的地方我从未在swift 2中发出此警告。
答案 0 :(得分:6)
将函数的返回值分配给_
以明确丢弃它,如下所示:
_ = somethingThatReturnsAValueYouDontCareAbout()
Swift 3警告未使用的函数返回值,因此您需要向它显示您知道自己在做什么。