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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #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

#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 #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS? #Jan 8 How to Install Xcode Command Line Tools on MacOS?
You might also like these
Understanding the confusing and handy “This” keyword in JavaScriptJavaScriptHow to check if the page is the home page in WordPress?WordPressHow to add Lifecycle Methods to a Class in React?ReactHow does while loop work in SCSS?SCSSReading Multiple Inputs in SwiftSwiftJavaScript Number MethodsJavaScriptThe Sort and Compare Functions in JavaScriptJavaScriptHow to change the style for checked radio button or checkboxes using CSS?CSSHow to use [] (square brackets) function in Envision Basic?Envision BasicWhat is XP (Extreme Programming) and When should it be used?Miscellaneous4 ways to create Date Objects in JavaScriptJavaScriptA Step by Step Guide to Make RSS in XML For Any Website or Blog For FreeMiscellaneous