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

最新文章

  1. ORACLE关闭启动的诡异错误
  2. bzoj1023: [SHOI2008]cactus仙人掌图
  3. BZOJ 2124: 等差子序列
  4. shared_ptr&lt;&gt; reset
  5. VSPM虚拟串口使用
  6. 配置SMarty解析
  7. Metadata Lock原理5
  8. oracle 字段上下两条记录的相减
  9. QT数据库使用案列【联系人】-- 使用sqlite和QStringListModel
  10. try、catch、finally与return
  11. C# 进销存系统开发框架
  12. 【LeetCode练习题】Unique Paths
  13. NSIS:判断程序是否运行并进行卸载
  14. [置顶] 实现360度全景图像的利器--PanoramaGL
  15. Mac关机时处于黑屏状态
  16. Session 和 Cookie 区别
  17. WEB实现单元格合并
  18. Centos7下mysql5.7.22主从配置
  19. TCP/UDP区别&amp;&amp;心跳包机制【转】
  20. eclipse从svn导入maven项目变成普通项目解决办法

热门文章

  1. 每天一个Linux命令(12):su命令
  2. idea在Maven Projects中显示灰色的解决办法
  3. JMeter学习笔记(九) 参数化4--User Variables
  4. [Mac]Mac OS X中WireShark的使用,及找不到网卡问题的解决方法
  5. capacilitys 持续集成
  6. c# 调用 matlab 引发初始化错误 异常
  7. hadoop datanode 和 tasktracker起不来
  8. [洛谷P2763]试题库问题
  9. 解析Mybaits的insert方法返回数字-2147482646的原因
  10. 用JavaScript写一个类似PHP print_r的函数