bundle.path(forResource: )December 15, 2016

In some cases, it makes sense to store a static JSON file inside the App bundle, in case the App is loaded without an Internet connection, but some aspects of the App can be used without networking. Here, we are grabbing a static JSON file "bundled-collections" directly from the app's bundle, and parsing it with JSONSerialization.

            
      func getBundledCollections() {
          
          let bundle = Bundle.main
          let path = bundle.path(forResource: "bundled-collections", ofType: "json")
          if let data:NSData = NSData(contentsOfFile: path!) {
              do {
                  let serializedJSON = try JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.allowFragments)
                  let json = JSON(serializedJSON)
                  
                  self.useSomewhere(json)
                  
              } catch {
                  
              }
          }
      }

          

The useSomewhere() function could update the data model, or pass the information to a view. I couldn't find a resource for Swift 3 to accomplish this, so I decided to quickly share it here.