Yogesh Chauhan's Blog

For Each Loop in Swift for Beginners

in Swift on May 3, 2020

forEach(_:)

It's an instance method that calls the given closure on each element in the sequence in the same order as a for-in loop.

Declaration


func forEach(_ body: (Element) throws -> Void) rethrows

Where body =  A closure that takes an element of the sequence as a parameter.

Example


let numberWords = ["one", "two", "three"]
numberWords.forEach { word in
    print(word)
}

//Output
one
two
three

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How JavaScript classes allows us to do less typing (syntactic sugar)?JavaScriptHAVING Clause in Postgres with Examples in All Aggregate FunctionsPostgresSlider animation using CSS transform propertyCSSHow to delete a local and a remote Git branches?MiscellaneousHow to select multiple values in React dropdown?ReactSolution to “Call to undefined function mysql_error()” in RevSlider WordPress PluginWordPressThe Differences Between HAVING Clause and WHERE Clause in SQLSQL/MySQLHow to load a module with configuration in SCSS?SCSSHow to check if the element exists using JavaScript and jQuery?JavaScriptCheck If a String Contains a Specific Word in PHPPHPHow to define visibility for a property in PHP?PHPWHERE Clause in PostgresPostgres