gpt4 book ai didi

objective-c - 在 Swift 中将参数传递给 Objective C 类

转载 作者:行者123 更新时间:2023-11-30 10:18:25 25 4
gpt4 key购买 nike

目标类别:

@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

我正在尝试使用此类来使用我的爱普生打印机进行打印。在我的旧 Objective-C 项目中,我可以毫无问题地打印它。

旧的 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];
}
}

在 Swift 中,我收到各种错误,例如“Int 无法转换为 Int32”、“Uint 无法转换为 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)

我已经尝试了几个小时并尽可能多地在 Google 上搜索,但我就是无法弄清楚...有人可以帮助我吗?

最佳答案

你尝试过吗:

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 &
)
  • 在 Swift 中,UIntInt32 是不同的类型,无法隐式转换。
  • sizeWritten 参数定义为 C 指针,可以在 Swift 中用 & 来描述。

关于objective-c - 在 Swift 中将参数传递给 Objective C 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com