, . :
1) NSMutableArray
import Foundation
func processRow(_ row : NSMutableArray, _ value : Int) {
for col in 0..<row.count {
row[col] = value;
}
}
func processMatrix(_ matrix : inout [NSMutableArray]) {
for rowIdx in 0..<matrix.count {
let r = matrix[rowIdx]
processRow(r, -2);
}
}
var matrix = [
NSMutableArray(array: [1, 2, 3]),
NSMutableArray(array: [4, 5, 6]),
NSMutableArray(array: [7, 8, 9])
]
processMatrix(&matrix)
print(matrix)
2)
class Wrapper<T: CustomDebugStringConvertible>: CustomDebugStringConvertible {
var value: T
init(_ value: T) {
self.value = value
}
var debugDescription: String {
return value.debugDescription
}
}
func processRow(_ row : Wrapper<[Int]>, _ value : Int) {
for col in 0..<row.value.count {
row.value[col] = value;
}
}
func processMatrix(_ matrix : inout [Wrapper<[Int]>]) {
for rowIdx in 0..<matrix.count {
let r = matrix[rowIdx]
processRow(r, -2);
}
}
var matrix = [
Wrapper([1, 2, 3]),
Wrapper([4, 5, 6]),
Wrapper([7, 8, 9])
]
processMatrix(&matrix)
print(matrix) // outputs [[-2, -2, -2], [-2, -2, -2], [-2, -2, -2]]
.