Reputation: 239
Let's say I want to close an USB device. Here is a C structure representing the USB device:
struct __USBDevice {
uint16_t idProduct;
io_service_t usbService;
IOUSBDeviceInterface **deviceHandle;
IOUSBInterfaceInterface **interfaceHandle;
Boolean open;
};
typedef struct __USBDevice *USBDeviceRef;
Here is the code to close the device:
// device is a USBDeviceRef structure
// USBDeviceClose is a function member of IOUSBDeviceInterface C Pseudoclass
(*device->deviceHandle)->USBDeviceClose(device->deviceHandle);
Now imagine that the device properties are declared in an obj-c class
@interface Device : NSObject {
NSNumber idProduct
io_service_t usbService;
IOUSBDeviceInterface **deviceHandle;
IOUSBInterfaceInterface **interfaceHandle;
BOOL open;
}
@end
How would I do to call USBDeviceClose() ?
Upvotes: 0
Views: 660
Reputation: 16861
No need to be redundant. Ivars can be structs.
@interface Device : NSObject {
USBDeviceRef deviceRef;
}
@end
#implementation Device
- (void) close {
USBDeviceClose(deviceRef->deviceHandle);
}
Upvotes: 0
Reputation: 33421
There are two ways. You can either model your class similar to a struct, and add @public
above your declarations (that way the syntax won't change) or you can add a Close
method to your interface which will do the same logic internally (but without the need to dereference device
of course).
Upvotes: 1