What Is the Practical Use of Nested Functions in Swift

What is the benefit of nesting functions (in general/in Swift)

So if the purported benefit is to "organize the code", why not just have the nested function independently, outside of the outer function? That, to me, seems more organized.

Oh, I totally disagree. If the only place where the second function is needed is inside the first function, keeping it inside the first function is much more organized.

Real-life examples here: http://www.apeth.com/swiftBook/ch02.html#_function_in_function

Plus, a function in a function has the local environment in scope. Code inside the nested function can "see" local variables declared before the nested function declaration. This can be much more convenient and natural than passing a bunch of parameters.

However, the key thing that a local function lets you do that you could not readily do in any other way is that you can form the function in real time (because a function is a closure) and return it from the outer function.

http://www.apeth.com/swiftBook/ch02.html#_function_returning_function

Swift - function inside function?

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.

Is self captured within a nested function

Unfortunately, only Closures have "Capture List" feature like [weak self]. For nested functions, You have to use normal weak or unowned variables.

func myInstanceMethod() {
weak var _self = self
func nestedFunction(result : Bool) {
_self?.anotherInstanceMethod()
}

functionExpectingClosure(nestedFunction)
}

Nested functions in Swift

No, that wouldn't really make sense. You can't access functions within functions. auto.isRegistered() returns a Bool, so auto.isRegistered().isNew() would try to call the isNew() method on Bool which, obviously, doesn't exist.

I also advise you to use computed properties instead of functions if you name it "isSomething", that's more in line with Apple's APIs. So you'd get something like this:

extension Auto {
var isRegistered: Bool {
// ...
}

var isNew: Bool {
// ...
}
}

Then you can simply check if an auto is both registered and new with

auto.isRegistered && auto.isNew

It is a good practice to use inner classes in swift for models?

This is a good practice to make your namespace organized and the question is related to the below one:
Swift nested class properties

Swift - Best practice to pass nested arrays into a function

Following the suggestions from this answer, you can try the following (Hints are in the code comments) :

var multiArray = [[1,2,3],[4,5,6],[7,8,9]]

// loop through your multiArray
for array in multiArray {

// Turning an array of Int into String with a separator between numbers
// In your case its only "" because you don't want to have separator like a dash for example
let stringArray = array.flatMap({ String($0) })
let stringRepresentationOfArray = stringArray.joinWithSeparator("")

print(stringRepresentationOfArray)
}

// Output
// 123
// 456
// 789

Method parameters in nested closure

You are correct, captured variables will last for the life of the closure. Here's an exert on capturing variables from apple's swift documentation:

A closure can capture constants and variables from the surrounding
context in which it is defined. The closure can then refer to and
modify the values of those constants and variables from within its
body, even if the original scope that defined the constants and
variables no longer exists.

In Swift, the simplest form of a closure that can capture values is a
nested function, written within the body of another function. A nested
function can capture any of its outer function’s arguments and can
also capture any constants and variables defined within the outer
function.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html



Related Topics



Leave a reply



Submit