http://stackoverflow.com/questions/24050844/swift-missing-argument-label-xxx-in-call

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.

asked Jun 5 '14 at 2:50
henry4343
1,5511817
 
2  
your function name is "say" yet you are calling sayHello?? –  Sam B Jun 5 '14 at 2:56
1  
    
I have been seen this question but it didn't solve my problem –  henry4343 Jun 5 '14 at 3:03
    
Is this a method? (A function w/in the scope of a class?) –  Logan Jun 5 '14 at 3:17

3 Answers

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 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: 

answered Jun 5 '14 at 4:37
Logan
15.7k62058
 
    
Funny how without providing the name for the second parameter in Playground seems to work beautifully! – user1107173 Oct 10 '14 at 4:56
1  
@user1107173 - That's because it's out of the context of a class. If you put it inside a class, it would provide the name for the second parameter. Playground doesn't have anything to do with it :) –  Logan Oct 10 '14 at 13:03

This is simply an influence of the Objective-C language. When calling a method, the first parameter of a method does not need to be explicitly labelled (as in Objective-C it is effectively 'labelled' by the name of the method). However all following parameters DO need a name to identify them. They may also take an (optional) local name for use inside the method itself (see Jiaaro's link in the comments above).

answered Jun 5 '14 at 3:22
Ephemera
3,18031752
 
    
I feel like instead of saying "does not need to be explicitly labeled" we could say "cannot be labeled" because adding a label there will result in a compilation error. –  ShaChris23 Jun 20 '14 at 6:15

This is a quirk in the compiler. Functions (which are not members of a class) and class methods have different default behavior with regards to named parameters. This is consistent with the behavior of named parameters in objective-C (but makes no sense for someone new to swift with no experience with objective-C).

Here's what the language reference has to say about named parameters for functions (specifically parameters where an external name for the parameter is not given, and the parameter does not have a default value)

However, these parameter names are only used within the body of the function itself, and cannotbe used when calling the function. These kinds of parameter names are known as local parameter names, because they are only available for use within the function’s body.

For information about class methods, see Logan's answer.

answered Jun 5 '14 at 3:39
user3386109
11.9k21227
 
    
It's actually not that simple. If you read further into the documentation, there are times when the internal name is automatically turned into an external name, from what I remember it's primarily within class definitions the 2nd and following tags are required by default. –  David Berry Jun 5 '14 at 6:10
2  
@David Agreed, my answer covers the case where the func is actually a function. Logan's answer covers the case where the func is actually a method. Personally, I don't like this design choice on Apple's part. The function/method calling syntax should be consistent, regardless of context. Programmers can use the #syntax to force the use of parameter names as a matter of style. –  user3386109 Jun 5 '14 at 6:28

Swift : missing argument label 'xxx' in call的更多相关文章

  1. Swift 1.0: missing argument label 'xxx' in call

    注意,这个问题是在swift1.0时发生的,swift2.0中,好像统一了function 和 method 的定义,具体待正式版发布后研究一下! 今天在使用swift时发现,写的func总是要求写出 ...

  2. linux报错 find: missing argument to `-exec'

    在linux下使用find命令时,报错:find: missing argument to `-exec' 具体执行命令为: find /u03 -name server.xml -exec grep ...

  3. Loadrunner 运行场景时:missing newline in XXX.dat 错误解决

    脚本参数化类型为file,在controller里运行场景的时候,报了个missing newline的错误,查了一下,将参数化的dat文件中的最后一行补上一个空行就解决啦!! 如果遇到此错误,需检查 ...

  4. swift学习之Label

              //UILabel的使用方法        let label:UILabel = UILabel(frame: CGRect(x: 0, y: 100, width: view. ...

  5. Missing associated label more...

    1.加上placeholder,可以为空 2.放在label标签中

  6. swift学习之label,button,imageView,字符串,数组,字典

    import UIKit class ViewController: UIViewController,UITextFieldDelegate { var textField: UITextField ...

  7. find: missing argument to `-exec'

    man find 发现 花括号要加 '' find ${LOG_BASE_DIR}$dir/ -type f -mtime +${KEEP_DAYS} -name ${LOG_REG} -exec r ...

  8. swift 动态获取label宽度或高度

    func getLabHeigh(labelStr:String,font:UIFont,width:CGFloat) -> CGFloat { let statusLabelText: NSS ...

  9. Swift函数_默认参数

    swift中提供了默认参数功能,在声明函数时给参数指定默认值. 例: func inputStudentInfo(name:String,age:Int="26") { print ...

随机推荐

  1. Vm Ubuntu 文件共享问题

    其实也是差不多的,就是需要重新安装一次工具 ,自己安装有问题,自己在手动安装一次就好了 下面是一个我的参考文章 http://blog.csdn.net/zz962/article/details/7 ...

  2. spring多个定时任务quartz配置

    spring多个定时任务quartz配置 <?xml version=”1.0″ encoding=”UTF-8″?> <beans xmlns=”http://www.spring ...

  3. 【java并发编程实战】第五章:基础构建模块

    1.同步容器类 它们是线程安全的 1.1 vector和hashtable. 和Collections.synchronizeXxx()一样.实现方式就是在每个方法里面加入synchronize代码块 ...

  4. javaX邮件发送

    /** * *  * @param mailServerHost 邮件服务器 * @param mailServerPort 端口 * @param validate 是否需要身份验证 * @para ...

  5. TW实习日记:第15天

    今天又是修修补补的一天,不过最开心的是因为项目比较特殊,有自己的后端服务器,有一些接口相关的bug可以让我直接写Java代码,终于可以碰一碰Java了哈哈.有好几个bug都是之前的人粗心设置了多余或者 ...

  6. POJ 2229 递推

    Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...

  7. linux进程同步之信号量

    首先了解一下,信号量机概念是由荷兰科学家Dijkstr引入,值得一提的是,它提出的Dijksrtr算法解决了最短路径问题. 信号量又称为信号灯,它是用来协调不同进程间的数据对象的,而最主要的应用是共享 ...

  8. SIP初步

    http://blog.sina.com.cn/s/blog_6b10255301012db7.html 1.什么是SIP SIP(会话发起协议)属于IP应用层协议,用于在IP网上为用户提供会话应用. ...

  9. hdu2421(数学,因式分解素数筛)

    Xiaoming has just come up with a new way for encryption, by calculating the key from a publicly view ...

  10. storm-kafka版本不匹配的问题

    storm集群是1.0.0版本,kafka的集群版本是0.10.0,所以想当然的使用了kafka_2.10-0.10.0.1.jar作为storm-kafka-1.0.0.jar的依赖使用, 没想到导 ...