views:

567

answers:

2

After four weeks of learning Objective-C for the iPhone, my first useful application is almost finished. However, I still need to save several instance variables whenever the current view is changed and to reload them when the view is reopened.

I have no trouble loading variables with basic C types like int and BOOL, but am having difficulty with a linked list made of Objective-C objects.

The linked list consists of memory accessed by three pointers: startPointer, currPointer, and endPointer. Each element consists of a last pointer, a next pointer, a pointer to a nested linked list (nestedPtr), and some variables.

My first attempt was to do the following in the loading code:

//dataStorageObj will contain the values to be loaded
dataStorageObj = [[DataSaver alloc] init];
NSData *data =[[NSMutableData alloc] initWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"Archive"]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
dataStorageObj = [unarchiver decodeObjectForKey:@"KeyForViewA"];
[unarchiver finishDecoding];
//****************************
//Simple variables loaded here
//****************************
//DataSaver is a class with methods to retrieve its instance variables
//The methods are named after the values they retrieve
startPointer = [dataStorageObj startPointer];
currPointer = [dataStorageObj currPointer];
endPointer = [dataStorageObj endPointer];
[unarchiver release];
[data release];

I placed a debug point at the end and all of the variables had the correct values. Actually accessing the values in the code (doing so in the console worked) resulted in *EXC_BAD_ACCESS* messages.


Next, I changed the linked list loading lines to the following:

startPointer = [[PointerClass alloc] initPointerFromList:[dataStorageObj startPointer]];
currPointer = [[PointerClass alloc] initPointerFromList:[dataStorageObj currPointer]];
endPointer = [[PointerClass alloc] initPointerFromList:[dataStorageObj endPointer]];

The initPointerFromList method:

-(PointerClass *)initPointerFromList:(PointerClass *)theList
{
    last = theList.last;
    nestedPointer = [[NestedPointerClass alloc] initNestedPointerFromList:theList.nestedPointer];
    //****************************************
    //Other variables loaded from theList here
    //****************************************
    if (theList.next != nil)
    {
     next = [[PointerClass alloc] initPointerFromList:theList.next];
    }

    return self;
}

The problem now was that the pointers were all pointing to individual linked lists, so I wrote methods to retrieve the start and end locations of the linked list referred to by currPointer:

currPointer = [[PointerClass alloc] initPointerFromList:[dataStorageObj currPointer]];
endPointer = [currPointer lastElement];
startPointer = [currPointer firstElement];


My question is threefold:

  1. Is it necessary to use methods such as initPointerFromList to allocate memory for a loaded linked list?
  2. Is there a more efficient way to maintain the relationship between currPointer, endPointer, and startPointer than by searching through the list using the lastElement and firstElement methods?
  3. Is there something wrong with the techniques behind the loading code I have presented? Without it, I have no problems; with it, I still get *BAD_ACCESS* messages. I want to know if these messages are tied to the loading code or if to the way I have handled object allocation throughout the rest of my application.

Thanks in advance!

+1  A: 

Pointers tend to be a sore point of archives, as you've discovered. Is it possible for you to use NSMutableArray for your storage requirements, as this might simplify matters for you.

http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047

If you're not going to use NSMutableArray, you probably want to create a container class that responds to the encodeWithCoder message, and encode the list that way. You're not going to store the pointers, but rather use them to walk your container, and archive it in some deterministic order.

Brian Mitchell
Thank you very much for your fast response. I will try to use NSMutableArray.
+1  A: 

What are you trying to gain by use of a linked list?

You are trying to shoehorn something from another language into Objective-C, and the results are way to much work for what you are wanting to do.

Either use an NSArray/NSMutableArray, or an NSDictionary/NSMutableDictionary with keys for specific elements you are trying to store.

Kendall Helmstetter Gelner
I was unaware that linked lists should be avoided in Objective C. I have not been coding in C-based languages long. I suppose that the only reason I was using a linked list was that it seemed easiest at the time. Sadly, the linked list is tied so deeply to my app that I will likely have to restart.
If use of a linked list specifically, instead of just an ordered collection, is tied so intimately to the rest of your application it's probably a good idea to take a step back and reconsider how you're developing it. If do real object modeling it shouldn't be tough to replace one for the other.
Chris Hanson
I don't see why you'd have to restart - the head is 0, the end element is the end of the array, and you could keep a pointer to your "current" element...Indeed, it would be easy to subclass NSArray to give you linked-list like accessors. You can't do it with a category to keep a current pointer.
Kendall Helmstetter Gelner
Also, it's not so much that you "need" to avoid linked lists, as there's just no foundation class for them - if you are going to be doing a ton of rapid inserts into the middle of the linked list, then I would say implementing one would be better than using NSArray.
Kendall Helmstetter Gelner