Bundle
Loading files at Run Time
Bundle
Loading files at Run Time
0
0
Checkbox to mark video as read
Mark as read

The Bundle class in Swift provides access to resources stored within a directory structure. When an app is built, various assets and files are packaged into its main bundle, which is essentially a container directory for all the resources needed by the app. You can access this main bundle using Bundle.main.

The Bundle class includes methods that help locate and load resources, making it an essential tool when working with mock data in development environments.

Creating a Mock Data File

We can use this to load mock data, let's make an example to load an array of users from a JSON file. Start creating a JSON file named user_mock_data.json with sample user data:

[
    {
        "id": 1,
        "name": "Alice",
        "email": "alice@example.com"
    },
    {
        "id": 2,
        "name": "Bob",
        "email": "bob@example.com"
    }
]

To include this file in the main bundle, drag and drop user_mock_data.json into your Xcode project, we created Resources folder just to have a better organization. Make sure that the file is added to the appropriate target so it will be accessible at runtime.


Loading JSON Data with Bundle

To load data from user_mock_data.json using Bundle, we need to:

  1. Locate the file in the main bundle with Bundle.main.url(forResource:withExtension:).
  2. Load the file's contents into a Data object.
  3. Decode the JSON data into a Swift model using JSONDecoder.

Let's start by defining a model that represents our data structure:

struct User: Codable {
    let id: Int
    let name: String
    let email: String
}

Now, we’ll create a function to load the JSON file and decode it into an array of User objects:

import Foundation

func loadMockData() -> [User]? {
    // Locate the user_mock_data.json file in the main bundle
    guard let url = Bundle.main.url(forResource: "user_mock_data", withExtension: "json") else {
        print("File not found")
        return nil
    }

    do {
        // Load data from the file
        let data = try Data(contentsOf: url)

        // Decode the JSON data into an array of User objects
        let users = try JSONDecoder().decode([User].self, from: data)

        // Return the result
        return users
    } catch {
        print("Error loading or decoding data: \(error)")
        return nil
    }
}

Generic solution

The function on the previous example will load only User objects, but since you'll need to load different types of objects, let's create a provider that will return a generic type taking the name of the json file as a parameter. Feel free to copy this to use it in your project.

struct JsonMockLoader<T: Decodable> {
    static func load(fileName: String) -> T? {
        // Locate the file in the main bundle
        guard let url = Bundle.main.url(forResource: fileName, withExtension: "json") else {
            print("File not found")
            return nil
        }

        do {
            // Load data from the file
            let data = try Data(contentsOf: url)

            // Decode the content and return the result
            return try JSONDecoder().decode(T.self, from: data)
        } catch {
            print("Error loading or decoding data: \(error)")
            return nil
        }
    }
}

Then you can load any type:

if let users: [User] = JsonMockLoader.load(fileName: "user_mock_data") {
    print(users)
}

if let products: [Product] = JsonMockLoader.load(fileName: "product_mock_data") {
    print(products)
}

Benefits of Using Bundle for Mock Data

Using Bundle to load mock data has several advantages:

  • Offline Testing: Mock data allows you to test the app without relying on a live backend.
  • Consistency: Mock data provides consistent data that makes testing and debugging easier.
  • Rapid Prototyping: You can quickly iterate on the UI and data structure without setting up an API.

0 Comments

Join the community to comment

Be the first to comment

Accept Cookies

We use cookies to collect and analyze information on site performance and usage, in order to provide you with better service.

Check our Privacy Policy