Printing the characters of a string value in Swift using the forEach
function or loop is easy. The forEach
function iterates over the string and repeats an action—in this case, it prints the character—for each element of the string.
string.forEach{
// print each character
print($0)
}
$0: This represents each character of the string as the loop executes.
// create a stringlet info = "Educative is the best!"// use forEach function// and print each characterinfo.forEach{// print each characterprint($0)}
forEach
function to print each character of the string.