UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

URL to local Bundle.main always returns nil

Forums > Swift

Overview I’m doing a simple singing game for kids. The game just plays a video and shows three options. I was updating the videos, so I deleted it from the Assets.xcassets then added the new ones.

The problem

Now I’m getting this error:

Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Volumes/Documentos/Trabajo/Codigo proyectos/Currents/HANDSUP/diloEnSenas/SingsCategoryView.swift, line 69
2020-05-12 14:03:46.769427-0500 HandsUP[12221:549710] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Volumes/Documentos/Trabajo/Codigo proyectos/Currents/HANDSUP/diloEnSenas/SingsCategoryView.swift, line 69

This is the code where I have the problem

struct video: View {
    var url: String

    var body: some View{
        VideoPlayerView(playerQueve: self.changeVideo(file: self.url))
    }

    func changeVideo(file: String) -> AVQueuePlayer {

        //let fileURL = file
        //let url = Bundle.main.url(forResource: fileURL , withExtension: "mp4")

        let url = Bundle.main.url(forResource: "color_1" , withExtension: "mp4")

        let playerItem = AVPlayerItem(url: url!)

        //set video

        let player = AVQueuePlayer(playerItem: playerItem)

        return player
    }
}

It was working fine before. So my first thought was that I’m not adding the files in the right way, but I checked it again and the buttons that are in the same folder as the videos are showing correctly.

Like you can see in the last image the button is showing well and the button image is in the same folder

also i check the build fase an the target also look good

Question

  • Why isn't it getting the file?
  • How can I debug the URL?

2      

I always use "Create Groups" when dropping files to the project instead of "Create folder references". I actually don't know the difference, but I was taught this way and that "Create folder references" won't let you see the files.

2      

let company = UserDefaults.standard.string(forKey: MyUserDefaults.Company)!
        print(company)
      let token = UserDefaults.standard.string(forKey: MyUserDefaults.Token)!
        print(token)
     let modifiedTime = UserDefaults.standard.string(forKey: MyUserDefaults.ContactsModifiedTime)!
        print(modifiedTime)

        //2020-08-21 07:31:35
  let url = URL(string: "https://\(company).hello.co/modules/RestfulApi/index.php?module=Contacts&token=\(token)&criteria=CE.modifiedtime:gte:\(modifiedTime)")!

gets this: Fatal error: Unexpectedly found nil while unwrapping an Optional value: file

Wonder if there is a length or something

This one works:

 let company = UserDefaults.standard.string(forKey: MyUserDefaults.Company)!
      let token = UserDefaults.standard.string(forKey: MyUserDefaults.Token)!
      let url = URL(string: "https://\(company).hello.co/modules/RestfulApi/index.php?module=Contacts&token=\(token)")!

2      

Doh... There was a space in my string!

 let modifiedTime = UserDefaults.standard.string(forKey: MyUserDefaults.ContactsModifiedTime)!
 print(modifiedTime)

  let encodedTime = modifiedTime.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
  print(encodedTime)   

    let url = URL(string: "https://\(company).hello.co/modules/RestfulApi/index.php?module=Contacts&token=\(token)&criteria=CE.modifiedtime:gte:\(encodedTime)")!

this now works without error

2      

See the "SENAS 2"

That space is jacking you up.

Encode the string then you should be good.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.