FileHandle(forWritingTo:) crash

I am having a rare crash when calling FileHandle(forWritingTo:) initializer with a file that does not exist.

In the documentation, I see that the expected behaviour is to return nil, but in my app, in rare cases I have a Crash.

My code that causes the crash is

if let handle = try? FileHandle(forWritingTo: logFile) {

Is it a known behaviour ? should I test if the file exist before calling FileHandle(forWritingTo:) ?

Thank you for sharing your code. I noticed that there is only one line of code in your post. Could you please create a simple, focused project that demonstrates the issue? If the crash occurs, it should indicate that thread 7 is the problem on the path of the string. However, I am not sure about the value of the logFile. Additionally, you are using a try? block, but if you have a corresponding catch block, it should prevent the crash. It would be helpful to have a focused sample project that I can download and examine the exact issue.

If so, please share a link to your test project. That'll help us better understand what's going on. If you're not familiar with preparing a test project, take a look at Creating a test project.

Albert Pascual
  Worldwide Developer Relations.

FileHandle(forWritingTo:) crash
 
 
Q