Some useful array tips from Swift 4
- 2020-06-03 08:33:30
- OfStack
preface
Swift provides two collection types to hold multiple values -- arrays (Array) and dictionaries (Dictionary). This we should all know, in the year before, bought Swift advanced (swift4.0), after the year is 1 little bit back learning, I have to say that meoshen wrote things are good, 69 yuan for the majority of programmers is not what. If you are interested in buying a book, really good
When I started learning arrays from the ground up, I found a number of functions that were really useful, so I won't go into more detail.
Mutable array technique in Swift 4.0
We can use Xcode to create playground as an exercise
First, create an array
let array = NSMutableArray(array: [1, 2, 3, 4 , 5, 6])
for in loops through
for x in array {
print(x)
}
1 2 3 4 5 6
Do you want to iterate over the remaining elements minus the first element?
for x in array.dropFirst(){
print(x)
}
2 3 4 5 6
dropFirst() function parameter is for x in ES50en. dropFirst(3) print :4 5 6.
Where there is first, there is almost always last
Want to iterate over elements other than the last three?
for x in array.dropLast(3){
print(x)
}
1 2 3
Traversal with subscripts and array elements
for (num, element) in array.enumerated() {
print(num, element)
}
Print left subscript right element
[
0 1
1 2
2 3
3 4
4 5
5 6
Left subscript and right element
conclusion