Swift:在调用中缺少参数标签'xxx' [英] Swift : missing argument label 'xxx' in call

查看:116
本文介绍了Swift:在调用中缺少参数标签'xxx'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  func say(name:String,msg:String){
println(\(name)say \(msg))
}

说(亨利,斯威夫特)< ----错误,因为在调用


我需要使用

 说(Henry,msg: 斯威夫特)

为什么?如果我在func中放入两个以上的var,那么当我调用这个函数时我需要编写var name而不是first var

这真的很麻烦,我在iBook Swift教程中没有看到任何解释。

解决方案

一个可能的原因是它实际上是一种方法。方法非常偷偷摸摸,它们看起来就像常规函数一样,但它们的行为方式不同,让我们看看这个:

  func funFunction(someArg:Int,someOtherArg:Int){
println(funFunction:\(someArg):\(someOtherArg))
}

//无外部参数
funFunction(1,4)

func externalParamFunction(externalOne internalOne:Int,externalTwo internalTwo:Int){
println(externalParamFunction:\(internalOne): \(internalTwo))
}

//需要外部参数
externalParamFunction(externalOne:1,externalTwo:4)

func externalInternalShared( #paramOne:Int,#paramTwo:Int){
println(externalInternalShared:\(paramOne):\(paramTwo))
}

//'' #'基本上说,你希望你的内部和外部名称是相同的

//注意,Swift 2中有一个更新,上面的函数必须写成一个s:

func externalInternalShared(paramOne paramOne:Int,#paramTwo:Int){
print(externalInternalShared:\(paramOne):\(paramTwo))
}

externalInternalShared(paramOne:1,paramTwo:4)

现在这里是有趣的部分,在类中声明一个函数,它不再是一个函数......它是一个方法

  class SomeClass { 
func someClassFunctionWithParamOne(paramOne:Int,paramTwo:Int){
println(someClassFunction:\(paramOne):\(paramTwo))
}
}

var someInstance = SomeClass()
someInstance.someClassFunctionWithParamOne(1,paramTwo:4)

这是方法行为设计的一部分



Apple Docs:


具体来说,Swift默认在方法中为第一个参数名称提供本地参数名称,并给出第二个和后续参数默认情况下,命名本地和外部参数名称。此约定与您在编写Objective-C方法时熟悉的典型命名和调用约定相匹配,并且无需限定参数名称即可进行表达式方法调用。


注意自动完成:


func say(name:String, msg:String) {
    println("\(name) say \(msg)")
}

say("Henry","Hi,Swift")  <---- error because missing argument label 'msg' in call

I need to use

   say("Henry",msg:"Hi,Swift")

Why ? If I put more than two var in func so that I need to write var name instead of first var when I call this func
It's really trouble, and I don't see any explain in iBook Swift tutorial.

解决方案

One possible reason is that it is actually a method. Methods are very sneaky, they look just like regular functions, but they don't act the same way, let's look at this:

func funFunction(someArg: Int, someOtherArg: Int) {
    println("funFunction: \(someArg) : \(someOtherArg)")
}

// No external parameter
funFunction(1, 4)

func externalParamFunction(externalOne internalOne: Int, externalTwo internalTwo: Int) {
    println("externalParamFunction: \(internalOne) : \(internalTwo)")
}

// Requires external parameters
externalParamFunction(externalOne: 1, externalTwo: 4)

func externalInternalShared(#paramOne: Int, #paramTwo: Int) {
    println("externalInternalShared: \(paramOne) : \(paramTwo)")
}

// The '#' basically says, you want your internal and external names to be the same

// Note that there's been an update in Swift 2 and the above function would have to be written as:

func externalInternalShared(paramOne paramOne: Int, #paramTwo: Int) {
    print("externalInternalShared: \(paramOne) : \(paramTwo)")
}

externalInternalShared(paramOne: 1, paramTwo: 4)

Now here's the fun part, declare a function inside of a class and it's no longer a function ... it's a method

class SomeClass {
    func someClassFunctionWithParamOne(paramOne: Int, paramTwo: Int) {
        println("someClassFunction: \(paramOne) : \(paramTwo)")
    }
}

var someInstance = SomeClass()
someInstance.someClassFunctionWithParamOne(1, paramTwo: 4)

This is part of the design of behavior for methods

Apple Docs:

Specifically, Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify your parameter names.

Notice the autocomplete:

这篇关于Swift:在调用中缺少参数标签'xxx'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆