-1

Following the wonderful post about IBInspectable / IBDesignable, I am reading the example code and can't understand how function drawRect contains another functions. Can somebody explain?

override func drawRect(rect: CGRect) {

    ....

    func rectForPosition(position: CGPoint, andSize size: CGSize) -> CGRect
    {
       ....     
    }

    ....

}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Luda
  • 7,282
  • 12
  • 79
  • 139
  • 4
    What is it that you don't understand, exactly? – jscs Mar 12 '18 at 12:46
  • 1
    https://stackoverflow.com/questions/32968133/what-is-the-practical-use-of-nested-functions-in-swift – Glenn Posadas Mar 12 '18 at 12:48
  • 3
    Possible duplicate of [What is the benefit of nesting functions (in general/in Swift)](https://stackoverflow.com/questions/30354534/what-is-the-benefit-of-nesting-functions-in-general-in-swift) – Tamás Sengel Mar 12 '18 at 12:48
  • A function declared in the body of a function (also called a local function) is available to be called by later code within the same scope, but is completely invisible elsewhere. -- Matt Neuburg, IOS 10 Programming Fundamentals with Swift (Third edition), p.41 – Stepan Valchyshyn Mar 12 '18 at 13:00

1 Answers1

3

rectForPosition is a function declared in scope of drawRect. Same as with variables, that means that rectForPosition is visible and usable only inside of drawRect.

Since as per language reference function declaration contains statements:

func function name(parameters) {
     statements
}

And a statement can be a declaration:

statement → declaration­

It is a valid Swift code.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90