How to return a mutable array from a function?
Here is a short snippet of code to make it more clear:
var tasks = ["Mow the lawn", "Call Mom"]
var completedTasks = ["Bake a cake"]
func arrayAtIndex(index: Int) -> String[] {
if index == 0 {
return tasks
} else {
return completedTasks
}
}
arrayAtIndex(0).removeAtIndex(0)
// Immutable value of type 'String[]' only has mutating members named 'removeAtIndex'
The following snippet works but I have to return an Array, not an NSMutableArray
var tasks: NSMutableArray = ["Mow the lawn", "Call Mom"]
var completedTasks: NSMutableArray = ["Bake a cake"]
func arrayAtIndex(index: Int) -> NSMutableArray {
if index == 0 {
return tasks
} else {
return completedTasks
}
}
arrayAtIndex(0).removeObjectAtIndex(0)
tasks // ["Call Mom"]
Thanks!