create a loop of strings ending at 10 in swift

To create a loop of strings ending at 10 in Swift, you can use a for-in loop that iterates over a range from 1 to 10. Inside the loop, you can create a string using string interpolation to combine a base string with the current value of the loop counter.

Here's an example code snippet:

main.swift
for i in 1...10 {
    let str = "String number \(i)"
    print(str)
}
70 chars
5 lines

In this code, the loop iterates over the range 1...10, which includes the numbers 1 to 10. Inside the loop, the str variable is created using string interpolation to combine the base string "String number" with the current value of i. The resulting strings will be:

main.swift
String number 1
String number 2
String number 3
String number 4
String number 5
String number 6
String number 7
String number 8
String number 9
String number 10
161 chars
11 lines

Finally, each string is printed to the console using the print() function.

related categories

gistlibby LogSnag