@IBAction func sliderMoved (_ slider: UISlider) - Want to understand a little more?

Hi All,

I’m wanting to understand in a little more detail what the code within the parentheses actually does? When I was watching the video, I didn’t quite latch on, and I’m trying to read into the things that I’m not 100% sure about… any assistance would be greatly appreciated…

@IBAction func sliderMoved (_ slider: UISlider)

Thanks, Michael

hi @anon7679634,
If your question is what does this line mean in detail, so here goes

@IBAction func sliderMoved ( _ slider: UISlider ) 
       1    2      3         4   5         6
  1. @IBAction -> This is a tag that tells Xcode that this function can be connected to an UIElement
  2. func -> this is used to define a function
  3. sliderMoved -> This is the name of the function
  4. _ -> This indicates the name of the parameter. generally a function that is defined as say
    func add(number1: Int, number2: Int) means that it takes two parameters and the first is called number1 and the other number2. The names are used both for internal (used inside the function) and external use (used for calling). The function would be called as add(number1: 1, number2: 2). If the function was defined as func add(_ number1: Int, number2: Int) then it is called as add(1, number2: 2) lastly, you can also define the function as func add(_ number1: Int, second number2: Int) and call this function as add(1, second: 2) where you call the function using the parameter name second where as internally the two parameters are referenced as number1 and number2.
  5. slider -> This is the internal name of the parameter that will be used int he function
  6. UISlider -> This is the type of the parameter, in the examples under #4. the parameters are of type Int and in this case it is of type UISlider.

hope that explains what you were after.

Cheers,

Jayant

Hi,

I saw different forms of @IBAction:

@IBAction func (sender: Type) {}
@IBAction func (_ sender: Type) {}

I’m curious how these different forms of method get called? Will swift automatically distinguish func(value) from func(sender: value)?

This topic was automatically closed after 166 days. New replies are no longer allowed.

@jiang If you define the function as func(sender: Type), you call it as func(sender: value).

If you define the function as func(_ sender: Type), you call it as func(value).

Please let me know if you have any other questions or issues about the whole thing when you get a chance.

Thank you!