gpt4 book ai didi

swift - 调用结果未使用

转载 作者:搜寻专家 更新时间:2023-10-31 22:30:19 26 4
gpt4 key购买 nike

在第二条评论的正下方,我收到错误消息“调用‘taskForDeleteMethod’的结果未使用。当我在调用后的闭包中使用结果和错误时,为什么会这样?

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)
}
}
}
}

最佳答案

早期的swift 版本 中,您无需担心方法的返回值。您可以将它存储在任何变量中,稍后使用它,或者您可以完全忽略它。它既没有给出任何错误也没有警告。

但在 swift 3.0 中,您需要指定是要忽略返回值还是使用它。

1。如果你想使用返回值,你可以创建一个变量/常量并将值存储在其中,即

let value = taskForDELETEMethod {
// Your code goes here
}

2。如果你想忽略返回值,你可以使用 _ ,即

let _ = taskForDELETEMethod {
// Your code goes here
}

关于swift - 调用结果未使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com