let stringArray: Array <String> = ["10", "20", "30", "40", "50"]
for index in stringArray {
println (index)
}
The output is as follows:
.
There is no problem using Swift to traverse the Array array, you can use for ... in loop.
(2) Declare an NSArray array and traverse, the code is as follows:
let stringNSArray: NSArray = ["10", "20", "30", "40", "50"]
for index in stringNSArray {
println (index)
}
The output is as follows:
.
It can be seen that the NSArray array can also be directly declared and traversed in Swift.
(3) Declare an NSArray array, convert to Array array, and then traverse:
let stringNSArray: NSArray = ["10", "20", "30", "40", "50"]
let stringArray: [String] = stringNSArray as! [String]
for index in stringArray {
println (index)
}
The output is as follows:
.
It can be seen that NSArray can be directly assigned to the Array array after type conversion, and then it can also be traversed.
(4) Declare an Array array, convert it to NSArray, and then traverse:
let stringArray: Array <String> = ["10", "20", "30", "40", "50"]
let stringNSArray: NSArray = stringArray
for index in stringNSArray {
println (index)
}
The output is as follows:
.
It can be seen that the Array array can also be directly converted into NSArray and traversed.
In summary, Swift is very compatible with NSArray in OC in terms of arrays. You can perform assignment conversion directly.
Copyright statement: This article is an original article by bloggers and may not be reproduced without the permission of the bloggers.
Swift Array array and NSArray array conversion and traversal