views:

8

answers:

1

Hi,

There is really something in objc I cannot understand. I know how to make retainable properties but cannot understand how to make retainable "sub" struc fields which are not "public" properties but only "private" properties (the private properties are only managed by my methods).

Here is my code:

    struct _device_descriptor {
    NSInteger accessoryNumber;                                    // Accessory the device belongs to
    NSInteger slotNumber;                                        // Slot number used for the device
    NSString* slotName;                                            // Slot name
};
typedef struct _device_descriptor device_descriptor_t;

#define NUMBER_MAX_CARD_READERS 10
#define NUMBER_MAX_ACCESSORIES 10
@interface CardDeviceManager : NSObject {
    // Card devices (among accessories)
    NSInteger m_numberOfCardDevices;                                // Number of devices.
    NSMutableArray* m_accessoryList;                                // List of all accessories
@private
    // Accessories
    NSInteger m_numberOfAccessories;                                // Number of accessories
    NSInteger m_numberOfAcceptedAccessories;                        
    device_descriptor_t m_cardDevice[NUMBER_MAX_CARD_READERS];        // Array of card devices.
}

I want the slot name (slotName in the struct) to be retained each time I assign it in my methods, but this is not a property since not visible from outside.

For example, each time I initialize it with another NSString, I do this:

//_tmpName is a NSString
// Warning: slotName must be released later since we retain it.
m_cardDevice[i].slotName = [[NSString stringWithString: _tmpName] retain];

I really feel this is not a "good" (not elegant) way of doing.

I think I should remove the _device_descriptor struct and have something like this:

NSInteger accessoryNumber[NUMBER_MAX_CARD_READERS];                   // Accessory the device belongs to
    NSInteger slotNumber[NUMBER_MAX_CARD_READERS];                    // Slot number used for the device
    NSString* slotName[NUMBER_MAX_CARD_READERS];     

But this is not better since I do not gather common things in a struct...

Is there a smarter way ?

Regards, Franz

+1  A: 

You're right; you can't do this via property syntax. I'd probably write C functions for getting and setting the struct fields, and then just make an arbitrary rule that accessing data from the structs in any way other than the functions is improper.

For example, I'd have:

device_descriptor_t* DeviceDescriptorCreate(NSInteger accessoryNumber,
                                            NSInteger slotNumber,
                                            NSString* slotName); //malloc()

void DeviceDescriptorDestroy(device_descriptor_t* device); //free()
void DeviceDescriptorSetSlotName(device_descriptor_t* device, NSString* slotName); //release old value, set new one
NSString* DeviceDescriptorGetSlotName(device_descriptor_t* device);

Et cetera.

Dave DeLong