문제

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

도움이 되었습니까?

해결책

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

다른 팁

No need to be redundant. Ivars can be structs.

@interface Device : NSObject {

USBDeviceRef deviceRef;
}

@end

#implementation Device

- (void) close {
USBDeviceClose(deviceRef->deviceHandle);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top