-1

I have a lot of JSON files that all of them are in a new folder called assets. How can I access some of the JSON files that are in a folder within the assets folder. Here is a screenshot of the file I want to work with. http://prntscr.com/eiv7p4

UPDATE: I can access mc-summer-0.json

here is the code with which I access the file "mc-summer-0.json"

if let path = Bundle.main.path(forResource: "mc-summer-0", ofType: "json") {
        do {
            let jsonData = try NSData(contentsOfFile: path, options: NSData.ReadingOptions.mappedIfSafe)
            do {
                let jsonResult: NSDictionary = try JSONSerialization.jsonObject(with: jsonData as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
                if let times_to : [String] = jsonResult["times_to"] as? [String] {
                    
                    for (value) in times_to {
                        print("\(value)")
                    }
                    
                }
            } catch {}
        } catch {}
    }

but if I want to access a file that is in the "assets" folder I change the line into if let path = Bundle.main.path(forResource: "assets/mc-summer-1", ofType: "json") { but unfortunately it doesnt work.

Community
  • 1
  • 1
Unique LoL Apps
  • 88
  • 1
  • 11
  • 1
    What have you tried? You need to post some of your relevant code and we can help diagnose a problem with it. – TheValyreanGroup Mar 12 '17 at 02:12
  • I've seen some tutorials but their json files are in the project and not within multiple folders. Here is the code they provide 'let url = NSBundle.mainBundle().URLForResource("Data", withExtension: "json") let data = NSData(contentsOfURL: url!)' – Unique LoL Apps Mar 12 '17 at 02:15
  • That will work as long as your JSON files are included in the copy bundle resources build phase. Check there. – TheValyreanGroup Mar 12 '17 at 02:17
  • I just added the assets folder which has own subfolders. I want to work with a json file that is within the subfolder. – Unique LoL Apps Mar 12 '17 at 02:19
  • What code have *you* tried? We don't care what some tutorial's code does. Post the code in your application that's trying to load these files. – Jim Mar 12 '17 at 02:46

2 Answers2

0

Those files are in groups in your project, not in separate folders. Very likely they will be in the root level of your app bundle, but we can't be sure based on what you've shown. I suggest building your app for the simulator and then opening the resulting bundle in the Finder and examining it.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I have the same issue like this question: http://stackoverflow.com/questions/1496373/iphone-get-the-file-path-which-is-within-subfolder-of-resource-folder I just need an answer for Swift 3 and all will be fine – Unique LoL Apps Mar 12 '17 at 02:59
  • This is really a comment, not an answer since it makes attempt to explain how to access the files in code. – rmaddy Mar 12 '17 at 02:59
  • @UniqueLoLApps No, you don't since your files are not in subfolder. They are simply in groups in your project. Not at all the same thing as a subfolder of your resources bundle. – rmaddy Mar 12 '17 at 03:00
  • @maddy well im new to ios programming i kinda dont get it. Can you guide me for a solution please? – Unique LoL Apps Mar 12 '17 at 03:04
-1

alright, in case somebody get stuck just like me the real solution is to remove the assets "folder" and re add it and select to add them as reference instead as a group

Unique LoL Apps
  • 88
  • 1
  • 11
  • That is not an assets folder, AND most importantly, myself and I'm sure everyone else thought you were talking about the Assets reference that is prebuilt with the project. You should not name Group the same as another group or reference. – TheValyreanGroup Mar 12 '17 at 13:53
  • sorry, i renamed the folder immediatelly – Unique LoL Apps Mar 12 '17 at 14:17