PDA

View Full Version : Beginner Question: NSArray, NSDictionary, Bindings & Objects of different 'types'.




breandan
Mar 3, 2008, 10:52 AM
Hi all,

I'm a beginner programmer with Cocoa, but I've programmed on Windows for a while. What I'd like to do is the following; I've created some simple classes, with each representing a 'profession' i.e.

1. Fireman - {name,age, salary| getName, getAge, getSalary etc...}
2. Policeman - {name, Rank, Dept}| getName, getRank, getDept etc...}

The classes contain different 'attributes' for each profession, and getter/setter methods for these attributes.

I'd like to have an array (or whatever is appropriate) of these objects, and use bindings to tie the array to the UI. The problem is that all these objects are not of the same type.

From what I've read, I think these are my choices:

1. Have an NSArray of NSDictionary, using the NSDictionaries to store the attributes instead of creating objects.

2. Create an NSDictionary and add these different objects. If this is an option, how would I determine the object type would I have to do something different for the bindings?

3. Have one 'controller'class, that would manage different arrays of objects i.e. One Array of Firemen, another array of Policemen etc

I can do this easily enough when all the objects are of the same type, but I'm not sure how to handle the different Object types.

Thanks for any information/thoughts you might have,

regards,

Brendan.



kainjow
Mar 3, 2008, 11:00 AM
Do you want to display both types of objects together in the same table view? If so, then you don't need dictionaries. Just put the objects into an NSArray, bind the array via NSArrayController to your table view.

You can use "name" for both since they both support it, but for any other attributes you'll probably want to use an NSValueTransformer. Look up the docs on that.

Also, not sure if you're doing this, but don't use "getName" for a getter. Use name and setName: if you're writing your own methods (or use @properties if you're in Leopard).

breandan
Mar 3, 2008, 11:28 AM
Hi,

Thanks for the quick reply.

Actually, I was hoping not to use a tableview at all; What I was hoping to do is have a tabview with 1 tab for each profession(tabs not visible); with each tab having a set of controls that can be used to set the attributes.

What I'd like to do is; when I 'load' an entry, the appropriate tab gets shown, and the controls on that page get filles with the attributes from the object.

Is this clearer?

Thanks for the info on the getter/setter methods. I haven't been doing this, but I'll start to.

Regards,

Brendan.