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

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
Use SwiperJS to create mobile touch sliders fastMiscellaneousHow to add Read More Read Less Button using JavaScript?JavaScriptColleague UI Basics: The Search AreaColleagueHow to create HTML elements with ID and class using JavaScript?HTMLPHP Login System using PDO Part 1: Create User Registration PagePHPHow to clean up after a plugin uninstall in WordPress?WordPressHow to create a function in SCSS (Sass)?SCSSHow to uninstall Cocoapods from the Mac OS?MiscellaneousHow to import MySQL small sample database into phpMySQL?SQL/MySQLTwo ways we can use colon(:) in Envision BasicEnvision BasicForcing the domain to serve securely using HTTPSMiscellaneousWhat’s the difference between visibility: hidden and display: none?CSS