March 2017
Intermediate to advanced
226 pages
5h 13m
English
Swift allows you to embed functions within each other. This encapsulates the embedded function and allows you to refactor otherwise repeating or abstractable code to a single call point. The Swift compiler is smart enough to inline and optimize these calls as needed.
Here’s an example of a nested function from Swift Foundation:
| | open class JSONSerialization : NSObject { |
| | /// Determines whether the given object can be |
| | /// converted to JSON. |
| | open class func isValidJSONObject(_ obj: Any) -> Bool { |
| | func isValidJSONObjectInternal(_ obj: Any) -> Bool { |
| | // Object is Swift.String or NSNull |
| | if obj is String || obj is NSNull { |
| | return true |
| | } |
| | ... |
When deciding whether to ...
Read now
Unlock full access