Swift - For in loop with array

Introduction

The For-In loop also works with arrays, as shown here:

Demo

var fruits = ["apple", "pineapple", "orange", "Json", "guava"]
for f in fruits {
    print(f)//from  w w w  . ja  va  2  s.  c o m
}

Result

The preceding example iterates through the five elements contained within the fruits array and outputs them:

You can also iterate through a dictionary, as the following example shows:

Demo

var courses =  [
   "A": "a",
   "B": "b",
   "C": "c"
]


for (id, title) in courses {
   print("\(id) - \(title)")
}

Result

You can iterate through a string and extract each character, as the following shows:

Demo

var str = "Swift Programming"
for c in str {/*from   ww w . j a  v a 2  s.  com*/
    print(c)
}

Result

Related Topic