| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
- GCD
- 아이폰7
- IOS
- UIView
- Bitcode
- swift3
- Example
- 신도림
- LG유플러스
- UITableView
- setting
- 포켓볼
- 얻는법
- 페이백
- 해몽
- 신도림 테크노마트
- swift
- Check
- loop
- error
- 샘플
- 공략
- 포켓몬 GO
- 스마트폰
- push
- 보라카이
- Xcode
- afterdelay
- simulator
- 앱스토어
- Today
- Total
도래울
swift for in loop 본문
For-In Loops
You use the for-in loop to iterate over a sequence, such as ranges of numbers, items in an array, or characters in a string.
This example prints the first few entries in the five-times table:
for index in 1...5 {print("\(index) times 5 is \(index * 5)")}// 1 times 5 is 5// 2 times 5 is 10// 3 times 5 is 15// 4 times 5 is 20// 5 times 5 is 25
The sequence being iterated over is a range of numbers from 1 to 5, inclusive, as indicated by the use of the closed range operator (...). The value of index is set to the first number in the range (1), and the statements inside the loop are executed. In this case, the loop contains only one statement, which prints an entry from the five-times table for the current value of index. After the statement is executed, the value of index is updated to contain the second value in the range (2), and the print(_:separator:terminator:) function is called again. This process continues until the end of the range is reached.
In the example above, index is a constant whose value is automatically set at the start of each iteration of the loop. As such, index does not have to be declared before it is used. It is implicitly declared simply by its inclusion in the loop declaration, without the need for a let declaration keyword.
If you don’t need each value from a sequence, you can ignore the values by using an underscore in place of a variable name.
let base = 3let power = 10var answer = 1for _ in 1...power {answer *= base}print("\(base) to the power of \(power) is \(answer)")// Prints "3 to the power of 10 is 59049"
The example above calculates the value of one number to the power of another (in this case, 3 to the power of10). It multiplies a starting value of 1 (that is, 3 to the power of 0) by 3, ten times, using a closed range that starts with 1 and ends with 10. For this calculation, the individual counter values each time through the loop are unnecessary—the code simply executes the loop the correct number of times. The underscore character (_) used in place of a loop variable causes the individual values to be ignored and does not provide access to the current value during each iteration of the loop.
Use a for-in loop with an array to iterate over its items.
let names = ["Anna", "Alex", "Brian", "Jack"]for name in names {print("Hello, \(name)!")}// Hello, Anna!// Hello, Alex!// Hello, Brian!// Hello, Jack!
You can also iterate over a dictionary to access its key-value pairs. Each item in the dictionary is returned as a(key, value) tuple when the dictionary is iterated, and you can decompose the (key, value) tuple’s members as explicitly named constants for use within the body of the for-in loop. Here, the dictionary’s keys are decomposed into a constant called animalName, and the dictionary’s values are decomposed into a constant called legCount.
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]for (animalName, legCount) in numberOfLegs {print("\(animalName)s have \(legCount) legs")}// ants have 6 legs// cats have 4 legs// spiders have 8 legs
Items in a Dictionary may not necessarily be iterated in the same order in which they were inserted. The contents of a Dictionary are inherently unordered, and iterating over them does not guarantee the order in which they will be retrieved. For more on arrays and dictionaries, see Collection Types.
'개발 > iOS' 카테고리의 다른 글
| Loop through subview to check for empty UITextField - Swift (0) | 2016.07.13 |
|---|---|
| remove all subviews of a view in Swift (0) | 2016.07.13 |
| swift uitableview checkbox example (0) | 2016.07.13 |
| Swift string replace (0) | 2016.07.12 |
| Swift 배열과 딕셔너리 (0) | 2016.07.12 |