NSPointerArray compact

  • Hi All,

    I'm using NSPointerArrays to store references for a complex object graph. The nodes themselves are stored in an NSDictionary for archiving, and the pointer arrays are just used to record the interconnectivity of the nodes (it's loopy and complex, which has been causing problems with NSKeyedUnarchiver). What I'm finding is that I'm getting nil references in my pointer arrays after unarchiving (which is a mystery I still have to solve), so I'd like to get rid of the nils before using the graph. However, calling -compact on them doesn't seem to work. No matter what I do, the nils are still in the arrays, and I'm really not sure what else I can do to get rid of them. The objects are instances of a custom class (i.e., not primitives) and I'm initializing with -pointerArrayWithWeakObjects.

    Any thoughts appreciated.

    J.

    James B Maxwell
    Composer/Doctoral Candidate
    School for the Contemporary Arts (SCA)
    School for Interactive Arts + Technology (SIAT)
    Simon Fraser University
  • On 27/07/2012, at 10:33 AM, James Maxwell wrote:

    > I'm using NSPointerArrays to store references for a complex object graph. The nodes themselves are stored in an NSDictionary for archiving, and the pointer arrays are just used to record the interconnectivity of the nodes (it's loopy and complex, which has been causing problems with NSKeyedUnarchiver). What I'm finding is that I'm getting nil references in my pointer arrays after unarchiving (which is a mystery I still have to solve), so I'd like to get rid of the nils before using the graph. However, calling -compact on them doesn't seem to work. No matter what I do, the nils are still in the arrays, and I'm really not sure what else I can do to get rid of them. The objects are instances of a custom class (i.e., not primitives) and I'm initializing with -pointerArrayWithWeakObjects.
    >
    > Any thoughts appreciated.

    One problem I've run into with NSPointerArray is that the API only allows you to recover the pointer (or remove it) by index. Since compacting changes the indexes, doing this means you lose track of the pointers unless you have some complicated means of tracking which pointer has which index, which means you need some additional data structure to do that... and basically it all ends up needlessly complex. So instead, I just don't compact the array and allow the nils to build up so the indexes remains static. I'm not archiving it though so next run I start over, and the total number of pointers is pretty small anyway.

    This might not have any bearing whatsoever on your problem, but I thought I'd share it anyway.

    It could be that -compact doesn't work (I don't know, because I don't use it for the above reason), or perhaps this is the wrong choice of container class for your case.

    --Graham
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today