Swift - Smart way to cycle through an Array of Array [[Int]]
You can do it this way:
let result = array.map { subarray in
subarray.map { integer -> Int in
if integer == 2 {
return 1
} else {
return integer
}
}
}
Even shorter:
let result = array.map {
$0.map { integer in
return integer == 2 ? 1 : integer
}
}
And a one-liner:
let result = array.map { $0.map { $0 == 2 ? 1 : $0 } }
I'll try to explain what's happening here in simple words: What map does is that it goes through the array elements one by one and applies a function on the element.
In our example, the first map iterates over the outer array elements, so $0
here refers to the inner arrays (one after one).
The second map iterates over the inner arrays' elements. So $0
in the inner map refers to the each element of the inner arrays' elements.
How to loop through two arrays with different amount of items? Swift
You can simply do,
var i = 0
for _ in 0..<min(alienArray.count, humanArray.count) {
print(humanArray[i].name, alienArray[i].numberOfLegs)
i += 1
}
print(humanArray[i...].compactMap({ $0.name }).joined(separator: " "))
print(alienArray[i...].compactMap({ $0.numberOfLegs }).joined(separator: " "))
How to iterate through multiple UILabels
You could put your labels inside another array and iterate through them:
let labelsArray = [positionTouch1LBL, positionTouch2LBL, positionTouch3LBL, positionTouch4BL, positionTouch5LBL]
for i in 0..<labelsArray.count {
// Get i-th UILabel
let label = labelsArray[i]
if touches.count >= (i+1) {
label.text = String(describing: touches[i].location(in: view))
}else{
label.text = "0.0 / 0.0"
}
}
This way you are able to group redundant code
How to loop through an optional collection
You have a couple of options. You can provide an empty array as the default value for elements
in a for ... in
loop:
let elements: [Element]?
for element in elements ?? [] {
}
Or you can use forEach
and optional chaining on elements
elements?.forEach { element in
}
Bear in mind that optional collections are an anti-pattern in Swift. You can represent the lack of value using an empty collection, so wrapping a collection in an optional doesn't provide any extra value, while complicating the interface.
Related Topics
Cannot Add Alamofire to Swift Project
Swiftui Coordinator Not Updating the Containing View's Property
Launch Sudo Command from MACos App Swift
Converting Swift 2.3 to Swift 3.0 - Error, Cannot Invoke 'Datatask' with an Argument List of Type'
Swift: How to Get Form Values Using Eureka Form Builder
My Uiviewcontroller Is Not Filling the Entire Screen
How to Access Firebase Variable Outside Firebase Function
Fblpromises Framework Not Found
How to Set an Environment Object in Preview
Compare Three Values for Equality
Why Is Swift's Ternary Operator So Picky About Whitespace
Swift [1,2] Conforms to Anyobject But [Enum.A, Enum.B] Does Not
How to Animate Bordercolor Change in Swift
Nsurlerrordomain with Code=-1100
How Are Hash Collisions Handled
Swift Combine How to Skip an Event
Ui Testing Failure - Neither Element Nor Any Descendant Has Keyboard Focus on Textview