2016-04-05 6 views
2

エラー:余分な引数ラベル「NO1:」通話中

class division { 
    var count: Int = 0 
    func incrementBy(no1: Int, no2: Int) { 
     count = no1/no2 
     println(count) 
    } 
} 

let counter = division() 
counter.incrementBy(no1:1800, no2: 3) 

下記のコードは、それがエラーを以下与え考慮してください。

error: extraneous argument label 'no1:' in call 
counter.incrementBy(no1:1800, no2: 3) 
        ^~~~~ 

私はラベルno1を削除すると、コンパイラは文句はありませんラベルno2の場合私は両方のレベルno1no2を削除すると

、それはエラーを以下与える:

error: missing argument label 'no2:' in call 
counter.incrementBy(1800, 3) 
       ^
          no2: 

誰もが、この動作を説明していただけます。前もって感謝します。

+0

あなたのSwiftバージョンは何ですか? no1を削除した後、私のために働いています。 – iMuzahid

+0

@ Md.Muzahidul No1を削除した後も、私のために働いています。 –

答えて

8

スウィフト3

この回答の内容は(スウィフト2.2 bbelowのために書かれた)すべてのほとんどは、まだ適用され、その差で、次のデフォルトの動作は、現在スウィフト3に保持していること:

  • すべて特記のない限り、関数パラメータは内部パラメータと同じ外部パラメータ名を持ちます。

つまり、最初の関数パラメータは、Swift 2.2の場合のように、デフォルトでは空の(省略された、_)外部パラメータ名を持たなくなりました。詳細については

、受け入れられ、実装は、以下を参照してくださいスウィフト進化提案:


スウィフト2つの

機能:外部および内部パラメータ名

Swiftの関数の場合、関数のパラメータは,のパラメータ名を持ちます。既定では、次の既定の動作が保持されます。

  • 最初の関数パラメーターには、空の(省略された、_)外部パラメーター名があります。
  • 特に指定がない限り、次のパラメータはすべて内部パラメータと同じ外部パラメータ名を持ちます。

そこで、以下の機能

func foo(first: Int, second: Int) {} 

が今

foo(1, second: 2) 
// |  \ 
// | by default, same external as internal parameter name 
// \ 
// by default, no external parameter name 

と呼ばれている、あなたは当然、最初の関数のパラメータは、その後にする必要がある外部名を(持っているかどうかを指定することができます関数を呼び出すときに指定されます)。

func foo(firstExternal first: Int, second: Int) {} 

// called as 
foo(firstExternal: 1, second: 2) 

同様に、外部名をアンダースコア_として指定することで、2番目のパラメータに外部名がないように指定することもできます(省略)。

func foo(first: Int, _ second: Int) {} 

// called as 
foo(1, 2) 

上記の最初の例(デフォルトの動作)に戻ると、外部名を指定しないと、

func foo(first: Int, second: Int) {} 

私たちは、次の明示的に指定外部名の同値である「デフォルト」関数のシグネチャ(WRT外部パラメータ名)を取得:

    を参照してください、詳細については

    func foo(_ first: Int, second second: Int) {} 
    //  |    \ 
    //  |  same external as internal parameter name 
    //  \ 
    // no (omitted) external parameter name 
    

  • The Language Guide - Functions

あなたの具体的な例を上から知識を使用して

、私たちはあなたの関数incrementByの署名を見て、あなたの具体的な例には、それを適用することができます。したがって

func incrementBy(no1: Int, no2: Int) { ... } 
//    |   \ 
//    | no explicitly stated external names: hence, since 
//    | this is not the first parameter, the external name 
//    | is set to the same as the internal name (no2), by default 
//    \ 
//  no explicitly stated external names: hence, since this is the 
//  first parameter, the external name is omitted, by default 

、我々はincrementBy

として、あなたの関数を呼び出します
incrementBy(1, no2: 2) 

これで、あなたの質問に含まれている2つの試み---エラーが発生する理由もわかります:

Error #1

error: extraneous argument label 'no1:' in call 
counter.incrementBy(no1:1800, no2: 3) 

As explained by this error message, you have an extraneous argument label for the first function argument: as covered above, the first function parameter has an omitted external parameter name by default (which is in effect in your example), and hence, when calling it, we should include no argument label for the first parameter.

Error #2

error: missing argument label 'no2:' in call 
counter.incrementBy(1800, 3) 

This attempted call, on the other hand, correctly omits external parameter name label for the first argument, but does so also for the second argument. The second function parameter of incrementBy , however, has the same external parameter name as its internal one, and hence, the external parameter name label no2: must be included in the call to the function.

+0

上記の説明は、メソッドではなく関数では有効です。 –

+0

@JayramKumarは、メソッドと(グローバル)関数の両方に適用されます。私が言及していない唯一のことは、すべてのイニシャライザパラメータがデフォルトで内部パラメータ名と同じ外部パラメータ名を持つ初期化子です(つまり、最初のパラメータにデフォルトでは省略された( '_')外部名はありません)。 – dfri

+0

'func foo(a:Int、b:Int) - > Int { return a + b } println(foo(2、b:3)); //エラー:無関係な引数のラベル 'b:' in call'、swift 1.2を使用しています。 –

関連する問題