0

Objective-Class:

@interface EpsonIo: NSObject {
@private
    void *handle_;
}

- (id) init;

- (void) dealloc;

- (int) open:(int)deviceType DeviceName:(NSString *)deviceName DeviceSettings:(NSString *)deviceSettings;

- (int) close;

- (int) write:(NSData *)data Offset:(size_t)offset Size:(int)size Timeout:(long)timeout SizeWritten:(size_t *)sizeWritten;

- (int) read:(NSMutableData *)data Offset:(size_t)offset Size:(size_t)size Timeout:(long)timeout SizeRead:(size_t *)sizeRead;

@end

I'm trying to use this class to print with my Epson printer. In my old Objective-C project, I can print it without any problems.

Old Objective-C:

// print document
EpsonIo *port = [[EpsonIo alloc] init];
if (port != nil)
{
    int errorStatus = EPSONIO_OC_SUCCESS;

    errorStatus = [port open:EPSONIO_OC_DEVTYPE_TCP DeviceName: @"192.168.1.168" DeviceSettings:nil];
    if (EPSONIO_OC_SUCCESS == errorStatus)
    {
        // settings for sending
        unsigned long sizeWritten;
        int errStatus;
        NSData *data = builder.data;

        errStatus = [port write:data Offset:0 Size:[data length] Timeout:100 SizeWritten: &sizeWritten];

        errorStatus = [port close];
    }
}

In Swift I'm getting various errors like "Int is not convertible to Int32", "Uint is not convertible to UnsafeMutablePointer"

Swift:

var printFile = EpsonIo()

        var errorStatus: Int32 = 0

        errorStatus = printFile.open(257, deviceName: "192.168.1.168", deviceSettings: nil)

        var printData: NSData = printText.data

        var sizeWritten: CUnsignedLong = 0

        var newStatus = printFile.write(printData, offset: 0, size: Int(printData.length), timeout: 100, sizeWritten: sizeWritten)

I've tried for hours and Googled as much as possible but I just can't figure it out... Can someone help me out?

1 Answer 1

1

Have you tried:

var sizeWritten: size_t = 0        // CUnsignedLong -> size_t

var newStatus = printFile.write(
    printData,
    offset: 0,
    size: Int32(printData.length), // UInt -> Int32
    timeout: 100,
    sizeWritten: &sizeWritten      // Add prefix &
)
  • In Swift, UInt and Int32 are different type and cannot be casted implicitly.
  • sizeWritten parameter is defined as a C pointer, which can be described & in Swift.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.