Simplifying this code?
Simplifying this code?
- Subject: Simplifying this code?
- From: "Theodore H. Smith" <email@hidden>
- Date: Tue, 27 Jul 2004 00:08:42 +0100
Heres an interesting problem. How to simplify this kind of code.
I have a window, with lots of controls on it, textfields, switch
buttons, color panels, etc.
The state of this window, which is defined by the state of the controls
on the window, can be saved as a string, and loaded in from a string.
Now, heres the ugly part. Within my NSView subclass, I have many many
IBOutlet object variables. I have one object variable I'd like to save
to the string which is not an IBOutlet.
So, to save it to a string, I need to create an array of items
implementing a serializable protocol. To create my array of items, I
need to hard code it like this:
MyArray = (id**)malloc( 12*sizeof(id*) );
Yuck. I really dislike magic numbers! But even if I were to replace 12
with a constant, its still not much better, because I must manually
change the number everytime I add or remove a control whose state is to
be saved.
Is there a better way??
My best guess would have been to do something like using a FieldOffset
macro, on the class... However, with the IBOutlet reordering and other
magic that the preproccessor does, I can't actually count on the fields
of my NSView subclass being in any particular order.
If I could somehow get a FieldOffset, then it would be very simple! I
could just in fact use my class as the array! A simple type cast of my
NSView subclass into an array of (id*)s, and no magic numbers would be
necessary to enter into the code.
--
Theodore H. Smith - Software Developer.
http://www.elfdata.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.