Program Tip

Swift : 호출에 인수 레이블 'xxx'가 없습니다.

programtip 2020. 10. 27. 23:09
반응형

Swift : 호출에 인수 레이블 'xxx'가 없습니다.


func say(name:String, msg:String) {
    println("\(name) say \(msg)")
}

say("Henry","Hi,Swift")  <---- error because missing argument label 'msg' in call

나는 사용해야한다

   say("Henry",msg:"Hi,Swift")

왜 ? 이 func를 호출 할 때 첫 번째 var 대신 var 이름을 작성해야하도록 func에 var를 두 개 이상 넣으면
정말 문제가되고 iBook Swift 튜토리얼에서 설명을 볼 수 없습니다.


한 가지 가능한 이유는 실제로 방법이기 때문입니다. 메서드는 매우 교활하고 일반 함수처럼 보이지만 동일한 방식으로 작동하지 않습니다. 다음을 살펴 보겠습니다.

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

// Note that there's been an update in Swift 2 and the above function would have to be written as:

func externalInternalShared(paramOne paramOne: Int, #paramTwo: Int) {
    print("externalInternalShared: \(paramOne) : \(paramTwo)")
}

externalInternalShared(paramOne: 1, paramTwo: 4)

이제 재미있는 부분이 있습니다. 클래스 내부에 함수를 선언하면 더 이상 함수가 아닙니다.

class SomeClass {
    func someClassFunctionWithParamOne(paramOne: Int, paramTwo: Int) {
        println("someClassFunction: \(paramOne) : \(paramTwo)")
    }
}

var someInstance = SomeClass()
someInstance.someClassFunctionWithParamOne(1, paramTwo: 4)

이것은 메서드 동작 설계의 일부입니다.

Apple 문서 :

특히 Swift는 메서드의 첫 번째 매개 변수 이름에 기본적으로 로컬 매개 변수 이름을 제공하고 두 번째 및 후속 매개 변수 이름에 기본적으로 로컬 및 외부 매개 변수 이름을 제공합니다. 이 규칙은 Objective-C 메서드를 작성할 때 익숙한 일반적인 이름 지정 및 호출 규칙과 일치하며 매개 변수 이름을 한정 할 필요없이 표현적인 메서드 호출을 만듭니다.

자동 완성을 확인하십시오. 여기에 이미지 설명 입력


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).


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 cannot be 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.


Swift 3.0 update:

In swift 3.0, methods with one param name per inputs are required to have that param name as part of the function call. So if you define the function like this

func say(name:String, msg:String) {
    print("\(name) say \(msg)")
}

Your function call will have to be like this

self.say(name: "Henry",msg: "Hi,Swift")

읽을 수있는 함수 레이블처럼 영어를 사용하고 싶지만 입력 매개 변수 이름을 변경하고 싶지 않은 경우 다음과 같이 매개 변수 이름 앞에 레이블을 추가 할 수 있습니다.

func say(somethingBy name:String, whoIsActuallySaying msg:String) {
    print("\(name) say \(msg)")
}

그럼 이렇게 불러

self.say(somethingBy: "Henry",whoIsActuallySaying: "Hi,Swift")

단순한:

잘못된 호출 함수 구문 (c / c ++ / java / c #에서는 동일하지 않음)

틀림 :

say("Henry")

옳은:

say(name:"Henry")

PS : 당신은 항상 해야합니다 ! 값 앞에 " name function parameter "를 추가하십시오 .


신속한 3+에서 이해하기 위해 작은 코드를 찾으십시오.

func sumInt(a:Int,b:Int){
    print(a+b) // Displays 3 here
}

sumInt(a: 1, b: 2) // not like in other languages

참고 URL : https://stackoverflow.com/questions/24050844/swift-missing-argument-label-xxx-in-call

반응형