I've been trying to use named pipes in iOS but it seems basically the same code in Swift fails while in ObjectiveC it works.
In Swift, the FileHandle for writing comes back nil, or if I use the FileHandle(forWritingAt: URL) API, it throws a Permission Denied. In Objective C the data is successfully sent through the pipe and logged.
The path in both of these is the exact same. The data in the Objective C example is also "hello".data(using: .utf8), though passed into the function.
Swift
do {
try FileManager.default.removeItem(at: url)
} catch let error {
print("error deleting", error)
}
mkfifo(url.path, 0777)
DispatchQueue.main.async {
let fileHandle = FileHandle(forWritingAtPath: url.path)
fileHandle?.write("hello".data(using: .utf8)!)
}
let fileHandle = FileHandle(forReadingAtPath: url.path)
if let data = fileHandle?.availableData {
print(String(data: data, encoding: .utf8))
}
Objective C
remove(path.UTF8String);
mkfifo(path.UTF8String, 0777);
dispatch_async(dispatch_get_main_queue(), ^{
NSFileHandle *fileHandle=[NSFileHandle fileHandleForWritingAtPath: path];
[fileHandle writeData:data];
});
NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath: path];
NSData* read = fileHandle.availableData;
NSLog([[NSString alloc] initWithData: read encoding: NSUTF8StringEncoding]);
These should just be calling the Objective C functions right? Why would they behave differently? XCode 11.3, iOS 13
Does Swift have octal numbers? I dont think so.
Swift does not have octal
I'm afraid Swift does have octal numbers, see Integer Literals
mkfifo(url.path, 0o777)