IOS statement syntax syntax

I am new to iOS and Swift. I am having a problem understanding the syntax used in protocol methods used in delegates. As an example, the following two methods are used in UIPickerView:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return count
}

      

The first method is good, but the syntax of the second method confuses me. The format of the second parameter is confusing, as I understand it must be an Int called "component", so why is it preceded by the action name "numberOfRowsInComponent"?

Also why are the delegate methods called "pickerView", are they all just overloaded?

Any guidance would be appreciated.

+3


source to share


2 answers


The fully qualified name of the method pickerView:numberOfRowsInComponent

, but component

is the name of the parameter that will actually contain the passed value

Read about External parameter names



It is sometimes useful to specify each parameter when calling a function to indicate the purpose of each argument that you pass to the function.

If you want users of your function to provide parameter names when calling your function, define an external parameter name for each parameter in addition to the local parameter name. You write the outer parameter name before the local parameter name it supports, separated by a space:

func someFunction(externalParameterName localParameterName: Int) {
    // function body goes here, and can use localParameterName
    // to refer to the argument value for that parameter
}

      

+2


source


numberOfRowsInComponent component

name is the external name and the second is internal. numberOfRowsInComponent

used in method invocation, but component

used in method implementation.

Also why are the delegate methods called "pickerView", are they just overloading everything?

They are not exactly overloaded as they have different signature names. for example



pickerView:numberOfRowsInComponent:
pickerView:widthForComponent:
// etc

      

This is an overloading method only if the method signatures are the same but have different parameter values ​​or types.

+1


source







All Articles