93sauu
93sauu

Reputation: 4107

Does it exist some way to have test data from json or swift files only for preview providers?

I would a way to have test data from JSON resources or swift files only for DEVELOPMENT. (I don't want to include them in release)

I have tried to use files without target membership but it does not work. Also, I tried using the DEBUG define to wrap the code but without luck.

Example:

struct BasketView_Previews: PreviewProvider {
    static var item: Item = {
        let item: Item = load(filename: "testItem")
        // or
        let item: Item = load(data: Data(testItemJSONString.utf8))
        return testItem
    }()

    static var previews: some View {
        MyView(item: item)
    }
}
#endif

Upvotes: 2

Views: 718

Answers (2)

93sauu
93sauu

Reputation: 4107

Steps:

  1. Create a folder for this kind of files.
  2. Create a .xcassets called for example "Preview Content".
  3. Make sure the target of the .xcassets is set.
  4. Add the folder in Development Assets.

Example 2 Example

Upvotes: 4

matt
matt

Reputation: 535306

You are looking for the DEVELOPMENT_ASSET_PATHS build setting. Resources at the designated paths are not present when you archive, so they won't be included in your distribution.

Here's the relevant paragraph from my book:

New in Xcode 11, the Development Assets build setting lets you specify a path for resources that won't be included in an archive build. In this way your app can include resources such as default data during development and testing, without those resources polluting the final built app. These can be individual resources or entire asset catalogs. A neat approach is a folder-linked group in the Project navigator: put your development-only resources into that group, and specify the path to the corresponding folder in Development Assets.

Upvotes: 2

Related Questions