Advanced Swift by Chris Eidhof & Ole Begemann & Airspeed Velocity

Advanced Swift by Chris Eidhof & Ole Begemann & Airspeed Velocity

Author:Chris Eidhof & Ole Begemann & Airspeed Velocity
Language: eng
Format: mobi, epub
Publisher: objc.io
Published: 2017-12-21T12:09:36+00:00


The doubler declared using the closure expression, and the one declared earlier using the func keyword, are completely equivalent, apart from the differences in their handling of argument labels we mentioned above. They even exist in the same “namespace,” unlike in some languages.

Why is the { } syntax useful then? Why not just use func every time? Well, it can be a lot more compact, especially when writing quick functions to pass into other functions, such as map. Here’s our doubler map example written in a much shorter form:

[1, 2, 3].map { $0 * 2 } // [2, 4, 6]



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.