In Swift Collections are pass by value by default and we can user inout
to make it pass by reference in function arguments but how can we do it in closure capture variables?
var list = [1, 2, 3]
func edit(inout list: [Int]) {
list.append(4)
dispatch_async(dispatch_get_main_queue()) {
list.append(5)
}
}
edit(&list)
...// after dispatch_async was executed
NSLog("\(list)")
Result will be [1, 2, 3, 4]
How can I modify the original variable () inside closure?
UPDATE:
Actually I have a workaround to handle this case by putting the array into an object so I can pass this object to the function by reference and we can modify the same array instance inside the function. but I want see any clever way to archive that