【问题标题】:Issue with calling function in swift [duplicate]swift中调用函数的问题[重复]
【发布时间】:2014-08-15 14:40:54
【问题描述】:

我正在关注可选值教程,其中我们有以下方法

  func findIndexOfString (string : String, array : String[]) -> Int?{           
            for (index, value) in enumerate(array){             
                if(value == string){
                    return index
                }               
            }           
            return nil          
        }

但是如果我调用这个方法

let indexFound = findIndexOfString("myString", neighbour) //neighbour is array of String

在调用中给出“缺少参数标签''array”的错误,这意味着我必须通过

来调用它
let indexFound = findIndexOfString("myString", array:neighbour)

是否必须在调用中提及参数标签?

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    是的。对于class methods,它是强制性的。您应该使用除第一个参数之外的参数名称。类方法和函数之间存在区别,对于您不会使用的函数(除非函数定义了外部参数名称,否则您不能使用)参数名称。

    【讨论】:

    • 在教程中,他们可以使用 let indexFound = findIndexOfString("myString", neighbour), without label
    • 如果他们这样使用,它可能是一个函数(在类之外)而不是一个方法
    猜你喜欢
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 2014-02-22
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    相关资源
    最近更新 更多