swift - how to avoid an object being deallocated? -
static func appendmissingobjtoarray(var oldarray:[anyobject],newarray:[anyobject]){ n in newarray{ var isexist = false o in oldarray{ if(n.isequal(o)){ //exist isexist = true break } } if(!isexist){ oldarray.append(n) } } }
the above function append data newarray oldarray. when function done. , getting data oldarray, got bad access error. think due newly added object in oldarray have been deallocated , newarray released.
any thing can avoid this?
i believe need declare oldarray parameter inout
. follows:
//... static func appendmissingobjtoarray(inout oldarray:[anyobject],newarray:[anyobject]){ //...
you can e.g (note ampersand &
below:
class01.appendmissingobjtoarray(&myoldarray, newarray: mynewarray) println(myoldarray) // contain appended result
done myoldarray
(the passed array) mutated. may need pass copy of original if retaining original important.
Comments
Post a Comment