gpt4 book ai didi

ios - 如何为具有命名参数的完成处理程序创建类型别名

转载 作者:行者123 更新时间:2023-12-04 15:00:53 24 4
gpt4 key购买 nike

我到目前为止是这样的:

  • 我已经定义了 typealias完成处理程序
    typealias UserCompletionHandler = (_ object: User?, _ error: ApiError?) -> Void
  • 我创建了一个使用此类型别名的服务功能
    func login(email: String, password: String, completion: UserCompletionHandler) {
    // ...
    // this part here handles API call and parsing logic
    // ...

    completion(user, nil)
    }

  • 我想要实现的是通过引入命名参数来获得更具可读性的带参数的完成回调。想法是这样结束:
       completion(object: user, error: nil)
    或者甚至更好地将错误参数设为可选,这样我就可以调用
       completion(object: user)
    问题是我找不到改变的方法 typealias定义来实现这一点。

    最佳答案

    显然这是不可能的。你可以在 swift 进化提案中找到这个选择背后的解释:0111-remove-arg-label-type-significance.md

    Function types may only be defined in terms of the types of the formal parameters and the return value.Writing out a function type containing argument labels will be prohibited

    关于ios - 如何为具有命名参数的完成处理程序创建类型别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66970045/

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