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 check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to apply style to the first-child using jQuery?jQueryHow to swap images on hover using CSS?CSSHow to get previous days or next days in PHP?PHPHow to get Current Year, Month and Date in JavaScript?JavaScriptUseful (and probably ignored) HTML tags: Part 2HTMLReading Multiple Inputs in SwiftSwiftAdvanced Array Methods in JavaScript (with examples)JavaScriptAlternate Style Sheets in CSSCSS5 Ways to Loop Through JavaScript ArraysJavaScriptLearn how to add Scroll Indicator using CSS and JavaScript?CSSHow to get the height and width of an element using JavaScript?JavaScriptHow to add onclick event to html elements dynamically using JavaScript?JavaScript