【问题标题】:Swift 3 - Is there any way to force method call on object?Swift 3 - 有没有办法强制对对象进行方法调用?
【发布时间】:2017-02-24 00:41:58
【问题描述】:

我制作了简单的 API 管理器。请求的方式是:

ApiManager
.get("method_name")
.parameters(["xxx" : "yyy"])
.requireAuth(true)
.request(success: success, failure: failure)

地点:

.get("people/1/") - 这个静态方法返回 ApiManager 对象

.request(success: success, failure: failure) - 这个方法启动请求

我想知道是否可以在 Swift 3 中强制调用这两个方法? 有什么方法可以防止调用 ApiManager 的初始化程序?

编辑

感谢您的回答。 我编辑了我的课程,现在 API 请求看起来是:

ApiManager(method: .get, action: "people/1/")
.parameters(["xxx" : "yyy"])
.requireAuth(true)
.request(success: success, failure: failure)

我将我的初始化设置为私有:

private init() {}

【问题讨论】:

  • 看来你的函数是类函数,没有调用ApiManager的初始化器。 (应该是ApiManager())。 “强制呼叫”是什么意思?他们没有接到电话吗?
  • 其实我的方法 .get("xxx") 是静态方法,里面有init,它返回ApiManager的对象。 “强制调用” - 我的意思是我想通知程序员他必须调用方法 .get()。不管怎样,谢谢你的回答。

标签: swift methods swift3 call


【解决方案1】:
class ApiManager {
    private init(){

    }
}

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多