Preface
Before the new year I bought Advanced Swift (Swift 4.0), and I’ve been gradually learning it since coming back. I have to say, what 喵神 writes is quite good. ¥69 is nothing for most programmers. If you’re interested, buy a copy — it’s genuinely good.
When I started learning arrays from scratch, I found many functions really useful.
Mutable Array Techniques in Swift 4.0
We can use Xcode to create a playground for practice.
First, create an array
1
2
let array = NSMutableArray(array: [1, 2, 3, 4 , 5, 6])
for-in loop iteration
1
2
3
for x in array {
print(x)
}
Output
1
1 2 3 4 5 6
Want to iterate over all elements except the first one?
1
2
3
for x in array.dropFirst(){
print(x)
}
Output
1
2 3 4 5 6
The dropFirst() function accepts a numeric parameter. For
for x in array.dropFirst(3), the output is: 4 5 6.
Where there’s first, there’s usually last
Want to iterate over all elements except the last 3?
1
2
3
for x in array.dropLast(3){
print(x)
}
Output
1
1 2 3
Iterate with index and element
1
2
3
for (num, element) in array.enumerated() {
print(num, element)
}
Output: index on the left, element on the right
1
2
3
4
5
6
7
0 1
1 2
2 3
3 4
4 5
5 6
Index on the left, element on the right
End of article