reports
reports copied to clipboard
FB11186889: -[NSURL initFileURLWithPath:] returns nil when initialized with an empty string
- Date: 2022-08-10
- Resolution: Open
- Area: Foundation
- OS: macOS
- Type: Incorrect/Unexpected Behavior
- Keywords: foundation, url, nil, crash
Description
When initialized with an empty string using the fileURLWithPath
, NSURL returns nil.
Unfortunately, the initializer is not marked as failable in Swift, leading to runtime crashes.
Consider the code below:
import Foundation
let url1 = URL( fileURLWithPath: "" )
let url2 = NSURL( fileURLWithPath: "" )
print( url1 )
print( url2 )
Here, both url1
and url2
are non-optional.
However, url2
will still be nil at runtime, leading to a crash in the last line, when trying to use the value.
Attached is a sample project demonstrating the issue.