本文介绍了无法快速加载viewDidLoad()中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法快速加载viewDidLoad()中的功能.它给出错误definition conflict with previous value" at "func joinString(#strings: String...) -> String {

Unable to overload function in viewDidLoad() in swift.It gives error definition conflict with previous value" at "func joinString(#strings: String...) -> String {

override func viewDidLoad() {
    super.viewDidLoad()
    func joinString(#strings: String[]) -> String {
            var str = "" 
            for s in strings {
                str += s
            }
            return str
        }
    func joinString(#strings: String...) -> String {
            return joinString(strings: strings)
        }
    println(joinString(strings : ["I", "am", "an", "array"]))
    println(joinString(strings : "I", "am", "an", "array"))
}

推荐答案

对我来说,这似乎是一个Swift错误(或未记录的限制).函数/方法重载通常可以正常工作,即使使用数组vs可变参数也是如此:

This looks like a Swift bug (or an undocumented restriction) to me.Function/method overloading works generally, even with array vs variadic parameters:

class MyClass {

    func foo(arg: Int) { println("An integer") }
    func foo(arg: Double) { println("A double") }

    func joinString(#strings: String[])  { println("An array") }
    func joinString(#strings: String...)  { println("Variadic parameters")}

    func test() {
        foo(2)
        foo(3.14)
        joinString(strings : ["I", "am", "an", "array"])
        joinString(strings : "I", "am", "an", "array")
    }
}

并产生预期的输出:


An integer
A double
An array
Variadic parameters

但是重载不适用于嵌套函数:

class MyClass {

    func test() {

        func foo(arg: Int) { println("An integer") }
        func foo(arg: Double) { println("A double") }
        // error: definition conflicts with previous value

        func joinString(#strings: String[])  { println("An array") }
        func joinString(#strings: String...)  { println("Variadic parameters")}
        // error: definition conflicts with previous value

        func test() {
            foo(2)
            foo(3.14)
            joinString(strings : ["I", "am", "an", "array"])
            joinString(strings : "I", "am", "an", "array")
        }
    }
}

这篇关于无法快速加载viewDidLoad()中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 03:49