本文介绍了通话结果未被使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第二个注释下面,我收到一个错误,调用结果'taskForDeleteMethod'未被使用。为什么当我在调用结束后使用结果和错误时?

  func deleteSession(_ completionHandlerForDeleteSession:@escaping(_ success:Bool,_ error:NSError?) - > Void){

/ * 1.指定参数,方法(如果有{key})和HTTP正文(如果POST)* /
//没有...

/ * 2.设定请求* /
taskForDELETEMethod {(results,error)in

/ * 3.将所需值发送到完成处理程序* /
if error = error {
print(Post error:\(error))
completionHandlerForDeleteSession(false,error)
} else {
guard let session = results![JSONKeys.session] as ?[String:AnyObject] else {
print(\(results)中的No key'\(JSONKeys.session))
返回
}

如果让id = session [JSONKeys.id]为? String {
print(logout id:\(id))
completionHandlerForDeleteSession(true,nil)
}
}
}
}


解决方案

/ code>变量,这实际上是在闭包中使用的,并且 taskForDELETEMethod 自身的结果是 NSURLSessionDataTask object。



从使用 taskForDELETEMethod 的例子,我可以在网上找到它看起来完全可以忽略返回值,因此可以通过将结果赋给 _ 变量来避免此警告,即

  let _ = taskForDELETEMethod {
... //其余的代码放在这里
}


Right below the second comment, I receive an error of "Result of call to 'taskForDeleteMethod' is unused. Why is this when I use the results and error in the closure following the call?

func deleteSession(_ completionHandlerForDeleteSession: @escaping (_ success: Bool, _ error: NSError?) -> Void) {

    /* 1. Specify parameters, method (if has {key}), and HTTP body (if POST) */
    // There are none...

    /* 2. Make the request */
    taskForDELETEMethod { (results, error) in

        /* 3. Send the desired value(s) to completion handler */
        if let error = error {
            print("Post error: \(error)")
            completionHandlerForDeleteSession(false, error)
        } else {
            guard let session = results![JSONKeys.session] as? [String: AnyObject] else {
                print("No key '\(JSONKeys.session)' in \(results)")
                return
            }

            if let id = session[JSONKeys.id] as? String {
                print("logout id: \(id)")
                completionHandlerForDeleteSession(true, nil)
            }
        }
    }
}
解决方案

You are confusing the results variable, which is, indeed, used inside the closure, and the result of the taskForDELETEMethod call itself, which is NSURLSessionDataTask object.

From the examples of using taskForDELETEMethod that I was able to find online it looks like it is perfectly OK to ignore the return value, so you can avoid this warning by assigning the result to _ variable, i.e.

let _ = taskForDELETEMethod {
    ... // The rest of your code goes here
}

这篇关于通话结果未被使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:01