Swift: missing argument label 'xxx' in call
2015-07-03 14:03
381 查看
http://www.cnblogs.com/breezemist/p/4326644.html
今天在使用swift时发现,写的func总是要求写出第二个参数的外部变量名,很不理解,感觉和书上说的function不一样,查了一下,终于发现了原因:写在class内部的function叫做method,是特殊的functoin,系统会自动补上外部变量名,参看以下连接 http://stackoverflow.com/questions/24050844/swift-missing-argument-label-xxx-in-call
防止连接失效,截取部分内容如下:
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:
Now here's the fun part, declare a function inside of a class and it's no longer a function ... it's a method
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.
今天在使用swift时发现,写的func总是要求写出第二个参数的外部变量名,很不理解,感觉和书上说的function不一样,查了一下,终于发现了原因:写在class内部的function叫做method,是特殊的functoin,系统会自动补上外部变量名,参看以下连接 http://stackoverflow.com/questions/24050844/swift-missing-argument-label-xxx-in-call
防止连接失效,截取部分内容如下:
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.
相关文章推荐
- Swift 闭包(六)
- Swift “ambiguous use of operator '>'”
- swift 之嵌套的理解 func chooseStepFunction(backwards: Bool) -> (Int) -> Int
- 苹果新的编程语言 Swift 语言进阶(七)--枚举、结构、类
- 苹果新的编程语言 Swift 语言进阶(六)--函数和闭包
- Swift 枚举(七)
- SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息
- Swift下调用Touch ID实现指纹识别
- Swift初体验(两)
- swift 之 模仿block 界面传值
- Swift 字典
- Swift基础学习(1)
- IOS Swift 学习之字符串与字符
- 苹果公司的新的编程语言 Swift 高级语言(十五)--协议
- Swift基本语法
- [教程技巧] 行走于Swift的世界中
- [教程技巧] 程序员眼中的苹果Swift语言
- [教程技巧] 关于Swift,开发者最需要了解的7个方面
- [教程技巧] Swift中下划线和"#"的妙用
- SWIFT推送之本地推送(UILocalNotification)