Array Creation in Swift

Level 287 keystrokes

Creates an array with a literal, prints each element with a for-in loop and then prints the count, 5.

Swift code

// Array literal; the type [Int] is inferred
1let nums = [1, 2, 3, 4, 5]
// num takes each element in order
2for num in nums {
// Prints 1 to 5, one per line
3 print(num)
4}
// count is the number of elements: Count: 5
5print("Count: \(nums.count)")

Output

1
2
3
4
5
Count: 5
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Swift code and how often they appear.

  • ,× 4
  • (× 3
  • )× 3
  • "× 2
  • :× 1
  • .× 1
  • [× 1
  • ]× 1
  • {× 1
  • }× 1

More Swift examples at level 2

← Swift typing practice