I'm new in swift and I want to know if the beast approach to reuse an array is to delete all object inside or reinitialize it.
For example if I have:
var my_array:NSMutableArray! //global variable
and the first method that I use:
func firstMethod{
my_array = NSMutableArray()
my_array.addObject("one")
my_array.addObject("two")
my_array.addObject("three")
...
}
for example now in another method I need my_array empty:
the best solution is this:
func newmethod() {
my_array = NSMutableArray() //reinitialize (drastic method)
//here, all new operation with my_array
}
or is this?:
func newmethod() {
if my_array == nil{
my_array = NSMutableArray()
} else {
my_array.removeAllObjects()
}
}
Why you are reinitializing the array there is no need to reinitialize it.And if you reinitialize then it will point to another memory address so it would be better to reuse without reinitialize. You can do like this
if (my_array == nil){
my_array = NSMutableArray();
}
else{
//my_array.removeAllObjects();
// Do your stuff here
}