accessing a file using [NSBundle mainBundle] pathForResource: ofType:inDirectory:

前端 未结 10 1348
清酒与你
清酒与你 2020-12-02 07:56

I have a file paylines.txt added inside the folder named TextFiles which resides inside the Resources folder of my iOS project in Xcod

相关标签:
10条回答
  • 2020-12-02 08:41

    I was also having the same problem. The Solution i found is ( in xcode 4.x):

    Go to : Target -> "Build Phases" -> "copy bundle Resources" Then add that particular file here. If that file is already added , delete it and add it again.

    clean the project and RUN. It works. :)

    0 讨论(0)
  • 2020-12-02 08:41

    Go to "Target" -> "Build Phases", select your target, select the “Build Phases” tab, click “Add Build Phase”, and select “Add Copy Files”. Change the destination to “Products Directory”. Drag your file into the “Add files” section.

    0 讨论(0)
  • 2020-12-02 08:44

    When I drag files in, the "Add to targets" box seems to be un-ticked by default. If I leave it un-ticked then I have the problem described. Fix it by deleting the files then dragging them back in but making sure to tick "Add to targets". enter image description here

    0 讨论(0)
  • 2020-12-02 08:45

    After following @Neelam Verma's answer or @dawid's answer, which has the same end result as @Neelam Verma's answer, difference being that @dawid's answer starts with the drag and drop of the file into the Xcode project and @Neelam Verma's answer starts with a file already a part of the Xcode project, I still could not get NSBundle.mainBundle().pathForResource("file-title", ofType:"type") to find my video file.

    I thought maybe because I had my file was in a Group nested in the Xcode project that this was the cause, so I moved the video file to the root of my Xcode project, still no luck, this was my code:

     guard let path = NSBundle.mainBundle().pathForResource("testVid1", ofType:"mp4") else {
            print("Invalid video path")
            return
        }
    

    Originally, this was the name of my file: testVid1.MP4, renaming the video file to testVid1.mp4 fixed my issue, so, at least the ofType string argument is case sensitive.

    0 讨论(0)
提交回复
热议问题