iOS How can I delete an Array from memory

grandM

macrumors 65816
Original poster
Oct 14, 2013
1,140
66
Ok, I can do array.removeAll()
However I still have an empty array then
I went array = nil but got an error "Cannot assign a value of type 'nil' to a value of type [Int]"
I'm puzzled :s
 

JWalker1995

macrumors regular
Mar 5, 2011
221
3
Why would you need to delete it completely from memory? If it's a property of the class then your can't 'delete it' you can only clear all the values so it is empty. If it's a local property in a method, then it will be deleted when the method ends, again I don't think you can delete it, just remove all the values from it.
 
  • Like
Reactions: akimoriRyuuji

1458279

Suspended
May 1, 2010
1,601
1,521
California
Isn't the 'array' just a pointer to memory? Correct me where I'm wrong, but it should be covered by ARC. If needed, you can check the type of hold it has (weak, strong, ...).
If it's a larger array and you want the memory, look into how and where it's held. You could check by calling a reference to the array and see if it's defined or not.
 

grandM

macrumors 65816
Original poster
Oct 14, 2013
1,140
66
Isn't the 'array' just a pointer to memory? Correct me where I'm wrong, but it should be covered by ARC. If needed, you can check the type of hold it has (weak, strong, ...).
If it's a larger array and you want the memory, look into how and where it's held. You could check by calling a reference to the array and see if it's defined or not.
it used to be a pointer, but setting it to nil no longer works
 

grandM

macrumors 65816
Original poster
Oct 14, 2013
1,140
66
Why would you need to delete it completely from memory? If it's a property of the class then your can't 'delete it' you can only clear all the values so it is empty. If it's a local property in a method, then it will be deleted when the method ends, again I don't think you can delete it, just remove all the values from it.
hmm you do make a point
still curious why it could be done in Objective-C and no longer in Swift
 

chown33

Moderator
Staff member
Aug 9, 2009
8,824
5,245
vertical
hmm you do make a point
still curious why it could be done in Objective-C and no longer in Swift
Post your code that declares the variable. If it's not declared as an optional, then try that.

You should probably review how Swift uses nil, and what it means for a var to be optional, as distinct from vars that aren't optional. Swift and Obj-C use nil in different ways.
 

grandM

macrumors 65816
Original poster
Oct 14, 2013
1,140
66
Post your code that declares the variable. If it's not declared as an optional, then try that.

You should probably review how Swift uses nil, and what it means for a var to be optional, as distinct from vars that aren't optional. Swift and Obj-C use nil in different ways.
Could you elaborate on that?
In my declaration I did not use an optional
I simply declared it as an Array of String which I initialized with an empty Array
Now you mention it, having declared it as an optional it should be possible to set it to nil
 

grandM

macrumors 65816
Original poster
Oct 14, 2013
1,140
66
Post your code that declares the variable. If it's not declared as an optional, then try that.

You should probably review how Swift uses nil, and what it means for a var to be optional, as distinct from vars that aren't optional. Swift and Obj-C use nil in different ways.
Apple seems to be a bit reluctant what nil is nowadays
they say it's an absence of value
 

AxoNeuron

macrumors 65816
Apr 22, 2012
1,240
847
The Left Coast
Apple seems to be a bit reluctant what nil is nowadays
they say it's an absence of value
As long as you no longer have any other objects referencing the array in your code then it should automatically dealloc itself because of automatic reference counting, assuming you aren't having an issue with a reference cycle.
 
  • Like
Reactions: jgelin

grandM

macrumors 65816
Original poster
Oct 14, 2013
1,140
66
As long as you no longer have any other objects referencing the array in your code then it should automatically dealloc itself because of automatic reference counting, assuming you aren't having an issue with a reference cycle.
this would imply it is still a pointer without reference
 

akimoriRyuuji

macrumors regular
Jun 28, 2015
115
84
this would imply it is still a pointer without reference
And what's the problem with that? You don't need to manually delete a pointer. That's all taken care of for you.

EDIT: Actually what is the point of deleting the array? Why bother even deleting the items in the array at all?
 

grandM

macrumors 65816
Original poster
Oct 14, 2013
1,140
66
And what's the problem with that? You don't need to manually delete a pointer. That's all taken care of for you.

EDIT: Actually what is the point of deleting the array? Why bother even deleting the items in the array at all?
I do not have a problem if it were a pointer
Problem is I can't find any reference stating it's a pointer
 

akimoriRyuuji

macrumors regular
Jun 28, 2015
115
84
Memory constraints!
Not necessarily. Depends on the lifetime of the array which leads me to my question...

I do not have a problem if it were a pointer
Problem is I can't find any reference stating it's a pointer
Forget pointers. Where is your array anyways? Inside a function, a class, global scope, etc.?
Because I took a look at this, I didn't see anything about it, except for that once an object has no reference it gets deleted (duh). Why would you waste time setting all the values to 0 or whatever.

I did find that you can set an array's members to nil if you use the '?' (optional) flag.

Code:
var myArray : int?[] = [nil, nil, 3]
But you're trying to clear up the memory taken up by the array in memory. You can't really do that. Question is what is the array for and is there a reason you have to delete its contents in the first place?
 
Last edited:

grandM

macrumors 65816
Original poster
Oct 14, 2013
1,140
66
Not necessarily. Depends on the lifetime of the array which leads me to my question...



Forget pointers. Where is your array anyways? Inside a function, a class, global scope, etc.?
Because I took a look at this, I didn't see anything about it, except for that once an object has no reference it gets deleted (duh). Why would you waste time setting all the values to 0 or whatever.

I did find that you can set an array's members to nil if you use the '?' (optional) flag.

Code:
var myArray : int?[] = [nil, nil, 3]
But you're trying to clear up the memory taken up by the array in memory. You can't really do that. Question is what is the array for and is there a reason you have to delete its contents in the first place?
The array is a Stored Variable Property in the ViewController. I guess making it an optional is the way to go. Giving the optional the nil value does not clear the memory entirely though as an optional is an enum anyways.
 

akimoriRyuuji

macrumors regular
Jun 28, 2015
115
84
Okay, I finally got to installing XCode and here's what I got.

Code:
var myArray : [Int]? = [0,1,2]
myArray = nil
So it appears you have to say that the variable is optional like I had written, except the syntax differs. As in, I wrote down an old version of Swift apparently.
So updated.

EDIT 2:
I did some talking with my friend. Well, I totally didn't think about it. But you're not supposed to have to worry about memory. In fact, you don't have any control over it at all. That's why Swift has Garbage Collection. You don't create pointers, or worry about stack/heap or whatever.

So you can set an array to nil but it's a waste. It's just an extra thing the app has to do which will slow down things. GC is your friend.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.