如何使用 Swift 获取 DispatchIO 的文件描述符?
How to get the file descriptor for DispatchIO using Swift?
我正在尝试将下面的 Objective-C 代码转换为 Swift。
-(void)openChannelWithURL:(NSURL*)anURL {
NSString* filePath = [anURL path];
self.channel = dispatch_io_create_with_path(DISPATCH_IO_RANDOM,
[filePath UTF8String], // Convert to C-string
O_RDONLY, // Open for reading
0, // No extra flags
dispatch_get_main_queue(),
^(int error){
// Cleanup code for normal channel operation.
// Assumes that dispatch_io_close was called elsewhere.
if (error == 0) {
dispatch_release(self.channel);
self.channel = NULL;
}
});
}
使用 DispatchIO
,init
需要一个文件描述符。如何获取文件描述符?
public mutating func openChannelWithURL(anURL: URL) {
let filePath: String = anURL.path
DispatchIO.init(type: .stream, fileDescriptor: ?, queue: DispatchQueue.global()) { code in
print("code: \(code)")
}
}
我正在尝试以只写模式打开文件,该模式会异步将数据附加到文件。
您没有文件描述符。你有一个文件路径。你调用了错误的初始值设定项。你想要这个:
https://developer.apple.com/documentation/dispatch/dispatchio/2892309-init
我正在尝试将下面的 Objective-C 代码转换为 Swift。
-(void)openChannelWithURL:(NSURL*)anURL {
NSString* filePath = [anURL path];
self.channel = dispatch_io_create_with_path(DISPATCH_IO_RANDOM,
[filePath UTF8String], // Convert to C-string
O_RDONLY, // Open for reading
0, // No extra flags
dispatch_get_main_queue(),
^(int error){
// Cleanup code for normal channel operation.
// Assumes that dispatch_io_close was called elsewhere.
if (error == 0) {
dispatch_release(self.channel);
self.channel = NULL;
}
});
}
使用 DispatchIO
,init
需要一个文件描述符。如何获取文件描述符?
public mutating func openChannelWithURL(anURL: URL) {
let filePath: String = anURL.path
DispatchIO.init(type: .stream, fileDescriptor: ?, queue: DispatchQueue.global()) { code in
print("code: \(code)")
}
}
我正在尝试以只写模式打开文件,该模式会异步将数据附加到文件。
您没有文件描述符。你有一个文件路径。你调用了错误的初始值设定项。你想要这个:
https://developer.apple.com/documentation/dispatch/dispatchio/2892309-init