1

In my project Navigator I have this structure

-MyApp
--ViewController.swift
--AppDelegate.Swift
--Main.StoryBoard
--info.plist
--JSONFiles
---test.json
-MyAppUITests

As you can see, this is the very basic structure that happens when you create a new single view application.

I created a new group called JSONFiles and added in a JSON files called test.

When I try to get the file using:

if let path = Bundle.main.path(forResource: "JSONFiles/test", ofType: "json") {
            do {
                let data = try NSData(contentsOf: URL(fileURLWithPath: path), options: NSData.ReadingOptions.mappedIfSafe)
                let jsonData : NSData = NSData(contentsOfFile: path)!
                allEntries = (try! JSONSerialization.jsonObject(with: jsonData as Data, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSArray

                print(allEntries)

            } catch let error as NSError {
                print(error.localizedDescription)
            }
        } else {
            print("Invalid filename/path.")
        }

I get the error:

Invalid filename/path.

If I move the JSON file our of the group folder and change the forResource to just "test" it works fine and prints in the console.

Can anyway tell me how to make it read from the folder? I could have all my JSON files in the root but I am wanting to tidy it up slightly.

Thanks

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
JamesG
  • 1,552
  • 8
  • 39
  • 86
  • try "test" as file name – Irfan Gul Oct 19 '16 at 11:57
  • Does `Bundle.main.path(forResource: "test", ofType: "json", inDirectory: "JSONFiles")` work? – Martin R Oct 19 '16 at 11:58
  • 1
    Did you check if the directory structure in the *compiled application bundle* is as you expect it? – Martin R Oct 19 '16 at 12:02
  • 1
    A group (yellow folder) is a virtual folder in the project hierarchy, it doesn't exist physically in the file system (unlike the blue folders, they are real folders). Your file is supposed to be in the folder `Resources`, not in any subfolder. – vadian Oct 19 '16 at 12:02
  • @MartinR I would not know how to check that, help? – JamesG Oct 19 '16 at 12:03
  • PS: And why don't you use the URL related API `url(forResource` since you need an `URL` anyway and the Swift 3 native structs `Data` and `URL` – vadian Oct 19 '16 at 12:11
  • @vadian I do not have any folders called 'Resources' – JamesG Oct 19 '16 at 12:11
  • Select the app under "Products" in the File navigator, then choose "Show in Finder". In the Finder choose "Show Package Contents" – Martin R Oct 19 '16 at 12:11
  • @vadian why do I need a URL? – JamesG Oct 19 '16 at 12:11
  • `URL(fileURLWithPath:` ?? Regarding `Resources`: Every standard Cocoa application has a top folder `Contents` and in there a folder `Resources`. It's created automatically and is not displayed in the project navigator. All file resources are located in that folder unless you explicitly create a **blue** subfolder. – vadian Oct 19 '16 at 12:13
  • @MartinR I think I followed your steps correctly. The son file is there but it is not in a folder. it appears to be in a main. Do I just create that folder there? – JamesG Oct 19 '16 at 12:15
  • I guess, while adding the folder to the project you need to ckeck "create Folder refrence" instead of "Create Groups". Just try with "create Folder refrence", It might work. Once I remember faced this kind of issue and resolved in this way. Let me know if it works – Janmenjaya Oct 19 '16 at 12:32
  • See for example http://stackoverflow.com/questions/10380842/uiimage-imagenamed-requires-pathforresource. – Martin R Oct 19 '16 at 12:40

1 Answers1

0

You must include your json file into "Copy Bundle Ressources". Go to your project target -> Build phases -> Copy Bundle ressources and there, add your json file.

Then, you should be able to retrieve the path using the function: Bundle.main.path(forResource: "test", ofType: "json") .

Youssef
  • 1
  • 2