0

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() ?

b1onic
  • 239
  • 2
  • 14

2 Answers2

1

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).

borrrden
  • 33,256
  • 8
  • 74
  • 109
0

No need to be redundant. Ivars can be structs.

@interface Device : NSObject {

USBDeviceRef deviceRef;
}

@end

#implementation Device

- (void) close {
USBDeviceClose(deviceRef->deviceHandle);
}
NSResponder
  • 16,861
  • 7
  • 32
  • 46