Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FB11186889: -[NSURL initFileURLWithPath:] returns nil when initialized with an empty string #567

Open
macmade opened this issue Sep 25, 2024 · 0 comments

Comments

@macmade
Copy link

macmade commented Sep 25, 2024

  • 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.

Files

URL.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant