zoukankan      html  css  js  c++  java
  • Swift 1.0: missing argument label 'xxx' in call

    注意,这个问题是在swift1.0时发生的,swift2.0中,好像统一了function 和 method 的定义,具体待正式版发布后研究一下!

    今天在使用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.

  • 相关阅读:
    判断大文件是否上传成功(一个大文件上传到ftp,判断是否上传完成)
    hbase的region
    把hdfs数据写入到hbase表
    eclipse和scala整合,打包配置文件及打包步骤
    sparkStreaming 读kafka的数据
    脚本put数据到hdfs
    Hive的自定义函数
    Ftp客户端需要TSL功能的文件上传
    Hive中的数据库、表、数据与HDFS的对应关系
    一文了解RPC框架原理
  • 原文地址:https://www.cnblogs.com/breezemist/p/4326644.html
Copyright © 2011-2022 走看看