gpt4 book ai didi

Swift - 重载静态方法

转载 作者:行者123 更新时间:2023-11-30 13:20:03 28 4
gpt4 key购买 nike

我一直在尝试在 Swift 2 中重载公共(public)类的静态方法。

这怎么可能?这与我想要实现的目标类似:

static func test(message: String) -> Void)?) { ### }

static func test(message: String, number: Int) -> Void)?) { ### }

最佳答案

目前还不清楚你到底想做什么,但有两个额外的右括号。看来您可能正在尝试采用结束论证?

如果意图是在第一个方法中获取 String 参数(在第二个方法中获取 StringInt)并返回 Void,那么你的语法应该如下所示:

static func test(message: String) { /* implementation */ }

static func test(message: String, number: Int) { /* implementation */ }

但是,如果目的是采用一个可选的闭包,该闭包采用 String 且不返回任何内容,则语法如下所示:

static func test(closure: ((String) -> Void)?) {
// implementation
}

然后,如果闭包也采用 Int:

static func test(closure: ((String, Int) -> Void)?) {
// implementation
}

关于Swift - 重载静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37858318/

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