How can I delete an Array from memory

Discussion in 'iOS Programming' started by grandM, Oct 11, 2015.

  1. grandM macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #1
    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
     
  2. JWalker1995 macrumors regular

    Joined:
    Mar 5, 2011
    #2
    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.
     
  3. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #3
    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.
     
  4. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #4
    it used to be a pointer, but setting it to nil no longer works
     
  5. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #5
    hmm you do make a point
    still curious why it could be done in Objective-C and no longer in Swift
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #7
    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
     
  8. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #8
    Apple seems to be a bit reluctant what nil is nowadays
    they say it's an absence of value
     
  9. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #9
    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.
     
  10. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #10
    this would imply it is still a pointer without reference
     
  11. akimoriRyuuji macrumors regular

    akimoriRyuuji

    Joined:
    Jun 28, 2015
    #11
    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?
     
  12. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #12
    Memory constraints!
     
  13. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #13
    I do not have a problem if it were a pointer
    Problem is I can't find any reference stating it's a pointer
     
  14. akimoriRyuuji, Nov 8, 2015
    Last edited: Nov 8, 2015

    akimoriRyuuji macrumors regular

    akimoriRyuuji

    Joined:
    Jun 28, 2015
    #14
    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?
     
  15. grandM thread starter macrumors 6502a

    grandM

    Joined:
    Oct 14, 2013
    #15
    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.
     
  16. akimoriRyuuji, Nov 11, 2015
    Last edited: Nov 11, 2015

    akimoriRyuuji macrumors regular

    akimoriRyuuji

    Joined:
    Jun 28, 2015
    #16
    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.
     

Share This Page