Swift - Nsurl Fileurlwithpath Not Unwrapped

Swift - NSURL fileURLWithPath not unwrapped?

Here's the issue : urlForScene returns a NSURL, whereas fileURLWithPath: returns an optional : NSURL?, (as per the doc).

So, the issue is, fileURLWithPath: might return nil (this is why it returns a NSURL?), and you return a non-nil object (a NSURL).

The compiler tells you to unwrap it, but I'd say you'd better return a NSURL? and check it later on in your code.

Plus, you shouldn't use NSObject.someInitializer(anObject), but NSObject(initializer:anObject). In your case, replace NSURL.fileURLWithPath(path)by NSURL(fileURLWithPath:path).

To summarize, here's the working code :

func urlForScene(sceneID:Scene) -> NSURL? {
let filename = "Whatever"

let path = NSBundle.mainBundle().pathForResource(filename, ofType: "m4a")
if let path = path {
return NSURL(fileURLWithPath:path)
} else {
return nil
}

/* this would also work, it's a matter of taste :
if let path = NSBundle.mainBundle().pathForResource(filename, ofType: "m4a") {
return NSURL(fileURLWithPath:path)
}
return nil
*/

}

let mySceneURL = urlForScene(someScene)
if let mySceneURL = mySceneURL {
/* use the scene */
} else {
/* the scene couldn't be intialized */
}

Unwrapping error in Optional value in file path

The URL is clearly a file system URL – indicated by a leading slash – so the API URL(string: is wrong. You have to use URL(fileURLWithPath which percent escapes space characters implicitly.

func openPDFFromInternet(www: String){
let serwerUrl = ApiConstans.fullPath + "" + www
let url = URL(fileURLWithPath : www)
print("the url = \(url)")
UIApplication.shared.open(url)
}

Or if the URL can be local or remote a more robust way

func openPDFFromInternet(www: String){
let serwerUrl = ApiConstans.fullPath + "" + www
if www.hasPrefix("/") {
let url = URL(fileURLWithPath : www)
UIApplication.shared.open(url)
} else {
guard let escapedString = www.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed),
let url = URL(string : escapedString) else { return }
UIApplication.shared.open(url)
}
}

Swift Impossible Type Inference

In Swift, Optional chaining like:

let path:String = url2?.path!

... is interpreted as:

let path:String = url2 != nil ? url2!.path!
: nil

As you see the type of path is non-Optional String, so the expression causes error.

(url2's type is URL?, so the type of property path is String, not String?.)


This is not a direct answer to your question, but I would re-write your code as:

let url = Bundle.main.bundleURL
let url2 = url.appendingPathComponent("foo.txt")
let path:String = url2.path

Shorter, and no worry about Optionals.

Swift3 : unexpectedly found nil while unwrapping an Optional value

I think the Bundle.main.path method returns an optional String. When that’s nil (because the resource was not found), force-unwrapping it causes your error. If you want to handle it correctly, you have to check for the nil:

guard let path = Bundle.main.path(…) else {
// resource not found, handle error
}

// now `path` is guaranteed to be non-nil
let alertSound = URL(fileURLWithPath: path)

Swift: Sound returns nil

I think the error is about using a forced unwrapping operator:

var enemy1sound = NSURL(fileURLWithPath: 
NSBundle.mainBundle().pathForResource("enemy1sound", ofType: "m4a")!)
^

If in your app logic it's possible and legit for the file to not exist, then I would protect that line of code with an optional binding:

if let path = NSBundle.mainBundle().pathForResource("enemy1sound", ofType: "m4a") {
let enemy1sound = NSURL(fileURLWithPath:path)
println(enemy1sound)

var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: enemy1sound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
}

If however that sound file is supposed to exist, and its absence is an exceptional case, it's ok to leave the forced unwrapping because that makes the error bubble up, although causing a crash. In such case I would look into why it's not found - such as, the file actually doesn't exist, etc.



Related Topics



Leave a reply



Submit