gpt4 book ai didi

swift - 有没有办法让一个函数在不重新指定所有参数的情况下调用自己?

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:17 25 4
gpt4 key购买 nike

如果有人能想出另一种方法来实现我正在尝试做的事情,欢迎提出您的想法!

为了给你一个想法,我有 40 多个不同的函数,它们的签名与这些类似:

func getXFromServer(arg1: String, arg2: Int, arg3: Bool, completion: ((MyCustomResponse)->Void)? = nil)
func getYDataFromServer(completion: @escaping (Bool)->Void)
func getZDataFromServer(arg1: Int, completion: @escaping (MyGeneric<MyClass>)->Bool)

有的有争论,有的没有;有些有一个完成处理程序,有些是可选的。传递各种类型。

我正在尝试简化处理服务器响应的过程:目前它有很多重复的代码,所有代码都做着几乎相同的事情,只是参数值和类型不同。

因为这些函数都是异步的,所以我使用完成处理程序来处理检索到的服务器数据。我希望所有“重复代码”都放在一个函数中,我们称它为 handleServerResponse:根据服务器响应的值,我想做更多异步操作并能够重新执行相同的外部函数

例如:

func getYDataFromServer(completion: @escaping (Bool)->Void)
{
session.dataTask(with: URLRequest()) { data, response, error in
handleServerResponse(){ valid //I want to pass 'getYDataFromServer(completion: completion)' here so that 'handleServerResponse' can determine whether to re-execute it or not, based on the server response

//do more stuff with Y server data
}
}.resume()
}

我目前正在处理 handleServerResponse 之外的重新执行,如下所示:

func getYDataFromServer(completion: @escaping (Bool)->Void)
{
session.dataTask(with: URLRequest()) { data, response, error in
handleServerResponse(){ valid
if !valid
{
self.getXYZDataFromServer(){
getYDataFromServer(completion: completion)
}
return
}
//do more stuff with Y server data
}
}.resume()
}

这是一个非常简单的代码版本,但正如您所看到的,它很快就会包含大量重复代码。

谢谢

最佳答案

也许这有帮助

  func getData(_ args: Any..., completion: @escaping ((Any)->Any)){
print(args)
completion(11)
}

但是函数类型不能强制转换为Any,安全类型可以考虑用enum混合。

            var valid: Bool = false
func getXFromServer(arg1: String, arg2: Int, arg3: Bool, completion: ((String)->Void)? = nil){}

func getYDataFromServer(completion: @escaping (Bool)->Void){
// session.dataTask(with: URLRequest()) { data, response, error in
// handleServerResponse(){ valid
valid.toggle()
print(valid)
if valid
{
MixGetData.XYZ.run{ MixCompletion.XYZ{ firstY.run{MixCompletion.Y(completion) } }}

}
else {
completion(false)
}
// }
// }.resume()

}

func getZDataFromServer(arg1: Int, completion: @escaping (String)->Bool){}
func getXYZDataFromServer(completion: @escaping ()->Void){
completion()
}

enum MixCompletion{
case X(((String)->Void)?)
case Y((Bool)->Void)
case Z((String)->Bool)
case XYZ(()->Void)
}

enum MixGetData{
case X( String, Int, Bool )
case Y
case Z(Int)
case XYZ

func run(completion: (() -> MixCompletion)? = nil){
if (completion == nil) {
switch (self) {
case let .X(arg1, arg2, arg3) : getXFromServer(arg1: arg1, arg2: arg2, arg3: arg3, completion : nil)
case let .Z(arg1) : getZDataFromServer(arg1: arg1, completion: {_ in return false})
case .Y : getYDataFromServer(completion: {_ in})
case .XYZ : getXYZDataFromServer(completion: {})
}}
else {
switch (self, completion!()) {
case (let .X(arg1, arg2, arg3), let .X(comp)): getXFromServer(arg1: arg1, arg2: arg2, arg3: arg3, completion : comp)
case (let .Z(arg1), let .Z(comp) ) : getZDataFromServer(arg1: arg1, completion: comp)
case (.Y, let .Y(comp)) : getYDataFromServer(completion: comp)
case (.XYZ, let .XYZ(comp)) : getXYZDataFromServer(completion: comp)
default: break
}
}
}
}
let firstY = MixGetData.Y

firstY.run()
firstY.run{MixCompletion.Y{bool in print (bool)}}

另一种方法是使用泛型函数。您也可以将两者结合起来:

      func getYDataFromServer(completion: @escaping (Bool)->Void){
// session.dataTask(with: URLRequest()) { data, response, error in
// handleServerResponse(){ valid
valid.toggle()
print(valid)
if valid
{
getData(name: "XYZ", array: "") { getData(name: "Y", array: "", completion: completion)}
}
else {
completion(false)
}
// }
// }.resume()
}

func getData<T>(name: String , array: Any... , completion: T ){
switch name {
case "Y":
getYDataFromServer(completion: completion as! (Bool)-> Void)
case "X":
let arg1 = array[0] as! String; let arg2 = array[1] as! Int; let arg3 = array[2] as! Bool
getXFromServer(arg1: arg1, arg2: arg2, arg3: arg3, completion: completion as? (String)-> Void)
case "Z":
let arg1 = array[0] as! Int
getZDataFromServer(arg1: arg1, completion: completion as! (String)-> Bool)
case "XYZ":
getXYZDataFromServer(completion: completion as! ()-> Void)
default:
break;
}
}

getData(name: "Y", array : "", completion: { bool in print (123) } as (Bool)-> Void )

我知道如果要输入的类型超过四种,那会很烦人。但这是编写安全代码的唯一方法。如果您运气好,请告诉我们。

如果你只是需要完成结果,也许这就是你想要的。

 func handleServerResponse<T, U>(_ alternative :  (T) -> () ,  _ completion : @escaping (U) -> (), _ handler : U , _ terminator : () , _ resValid : Bool){
if resValid {
alternative( completion(handler) as! T )
}
else {
terminator
}
}


func getYDataFromServer(completion: @escaping (Bool)->Void){

response.toggle()


// session.dataTask(with: URLRequest()) { data, response, error in


handleServerResponse({(a) in getXYZDataFromServer {a
}}, { (a: @escaping (Bool)->Void) in getYDataFromServer(completion: a) }, completion, completion(true), response)

             handleServerResponse( { (a)   in getXYZDataFromServer{a}} , {  (a:  @escaping (Bool)->Void)   in getZDataFromServer(arg1: 1, completion: { (s) -> Bool in
a
return false
})}, completion, completion(true), response)

它对我来说效果很好。

关于swift - 有没有办法让一个函数在不重新指定所有参数的情况下调用自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55252827/

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