Читайте также: |
|
- Swift, being a C influenced language, has the keywords you would probably expect for creating loops. We've got while, do while, and for. Although, there are two kinds of for loops in Swift. First, if you're familiar with the classic C style for loop with the initializer, condition, and increment all set up at the top, separated by semicolons, you can certainly do this in Swift. I'm not actually going to say much about this one, but the only difference is you don't need the parenthesis around the three parts of this. Of course, you'll be using Var to set up an index variable, because while this version does work fine, there is an alternative in Swift that's often preferable, which is the for-in loop.
This works great for iterating over a sequences of numbers, works wonderfully with any collection, like going through all the elements in an array, or even all the characters in a String. So, the basic format of a for-in loop is for each item in some collection. So, for each number in a range of numbers, for each element in some array. So, here's an example of a loop that iterates from one to 100. I'm using the name index here, so for index in 1...100, but this name is up to us.
If we're iterating over a range of numbers, Swift lets us use any name. This could be index, I, idx, (fu), X, whatever. It recognizes this will need to be an integer, so each time around the loop this integer variable would contain the current value, in this case from one to 100. I am using a Swift range operator, again, the three dots we saw previously with the switch statement. So, the three dots is the closed range operator. It creates a sequence that includes the numbers on both side of the three dots.
This is a closed inclusive range. When you are iterating around and using an index, it is common to want ranges that start at zero, particularly, when working with zero based collections like arrays. We can certainly do that with the closed range operator here. I would just be changing the first number to a zero. So, this would be zero through 100 including both zero and 100. This would be 36 through 99, including both of those numbers, or we could go zero through some variable, like the length of an array.
I mentioned earlier that there were two range operators in Swift. So, the other one, instead of three dots, we use two dots and a less than sign. This still creates a range using the numbers on either side. The only difference between the two, and you can remember this from the less than sign, is that the half-open range operator, the dot dot less than, does not include the number on the right. It goes up to one less than the number on the right. So, this would go from zero to 99, including both zero and 99, from 36 to 98, from zero to one less than the count of an array.
The main reason for this second version, the main reason for the half-open range operator is that, when we work with zero based collections like arrays, we typically can get the length of the array, however many items are in that. So, we will want a loop that can start at zero but go up to one less than the current length of the array. This is the way that you would do it. For-in loops don't actually have to go through a sequence of integers. I could also write something like this, to create a String of our name equal to Bob, then say for each Char in name.
Swift will look at this, realize we're not iterating of a range of numbers, we are treating a String as a collection, and it will take it as a collection of characters. It'll take each letter in turn in String and just write those out. Notice, that we don't have to keep track of an index. We don't have to keep track of the length of the String, it just works. As we'll see when we cover arrays in dictionary's, for-in loops just work with those as well. Moving onto while loops. Now, there's nothing remarkable about while loops in Swift.
It's about as simple as it gets. The format is the very straight-forward simple one you would see everywhere else in any other C style language. The keyword while, then your condition, then a set of curly braces. The most noticeable difference is that then, again, just like if statements in Swift, you do not need parenthesis around your condition. You do, not surprisingly, need curly braces around the body of the loop. As with if statements, the condition must evaluate is true or false, and as long as it's true, we will just keep on doing whatever's in the curly braces.
Whatever's in the body of the loop. Other than that, pretty standard while loop. There is also the do while loop in Swift. As with other languages, we're removing the condition to the end of the loop. As in other languages, the one important factor always remember about do while is that we will always execute the body of the loop as least once, before the condition is ever looked at. So fairly conventional stuff there. Let's go on to talk about functions, because functions in Swift certainly are different from many other languages.
Дата добавления: 2015-08-20; просмотров: 95 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Using the switch statement Использование Переключатель | | | Creating loops in Swift Создание петель в Swift |