1

What does the (_:for:) syntax mean after the draw text? In particular the underscore and for. I would assume the two colons are meant to represent that the method takes two arguments.

I just picked this method as an example from https://developer.apple.com/documentation/uikit/uiview/1621844-draw

Additionally, there is a method draw(_:) and how does this differ from draw(_:for:)?

This is from https://developer.apple.com/documentation/uikit/uiview/1622529-draw

I am unable to clearly discern what the differences are from reading the documentation.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
sgx
  • 1,265
  • 3
  • 19
  • 36

1 Answers1

2

1- draw(_:for:)

Implemented to draw the view’s content for printing.

2- draw(_:)

Draws the receiver’s image within the passed-in rectangle.

1- The _ is the argument label and when it exists for a parameter you don't need to pass the parameter name when you call the method

e.x

func goTo(value:Int)  // call goTo(value:5)
func goTo(_ value:Int)  // call goTo(5)

2- The colon : separates the parameterName from the parameterType like

paramterName:parameterType

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Ok thanks - I understand that there are differences bw the 2 methods so I'm more asking about the formatting of how they're listed. So, what you're saying is when there is an underscore after the parentheses for a method, that underscore represents an optional parameter that doesn't necessarily need to be passed to the method? – sgx May 24 '19 at 16:10
  • 1
    @user2603139 when you write _ you can't type parameter name check **Function Argument Labels and Parameter Names** section in https://docs.swift.org/swift-book/LanguageGuide/Functions.html – Shehata Gamal May 24 '19 at 16:19
  • “The colon `:` separates the parameterName from the parameterType like” ... Yes, where you implement the method, it separates the parameter name from the parameter type. But in the `draw(_:for:)` and `draw(_:)` syntax that the OP was asking about, this syntax we see in documentation, the colon obviously serves a completely different purpose, i.e. it now it namely it follows each argument label, e.g. `draw(_:for:)` has two parameters, the first has no argument label (indicated by the `_`) and the second parameter’s argument label is `for`. The colon in this case follows each argument label. – Rob May 24 '19 at 17:18
  • @Rob we should admit that this short re-cap of method they do is misleading when you look to it at the first time , they give it as a quick pre-look to the actual syntax as a mini-header function where number of colons is useful for knowing number of parameters and whether it has _ s or not BTW it's a documentation style that should be ignored when it's time for a newbie to learn – Shehata Gamal May 24 '19 at 17:38
  • 2
    Agreed. I was just pointing out that the OP was asking about what the `draw(_:for:)` syntax meant, with its confusing use of colons, and you proceeded to explain a completely unrelated use of a colon. Lol. – Rob May 24 '19 at 17:42