how to read a file under project directory for both development and runtime?

For example I created a project aaa. aaa source folder is called aaa. that is

aaa
aaa/aaa

then I put a data file in

aaa/aaa/some.data

now i want to test data in

aaa/aaaTests/aaaTests.swift

How can i access aaa/aaa/some.data in aaaTests.swift?

If I want to access some.data inside aaa/aaa/ContentView.swift, how to write the code? I have been searching google and bing for days, but find no example answering my questions. Can anyone help me out? Thanks in advance.

Accepted Reply

It's worth bearing in mind that your file structure for development is different to the file structure for the compiled application. some.data may not be at the same level as ContentView.swift, or even available to your application by default.

The best way to achieve what you need is to setup Xcode to copy the some.data file into your bundle as a resource, which you can then access at runtime.

Add the file to the application bundle

  1. In Xcode, select your Project in the project navigator.
  2. Find the application target for your project in the Targets list. This will usually have a little app icon.
  3. Navigate to "Build Phases".
  4. Expand the "Copy Bundle Resources".
  5. Press the "+" button and in the dialog that opens, find your some.data file, and click "Add".

Add the file to the test bundle

The steps are very similar for adding it to the testing target.

  1. In Xcode, select your Project in the project navigator.
  2. Find the test target for your project in the Targets list. This will usually either have a little icon with four squares, or a little with two squares and a slider, depending on whether you are doing UI tests or code tests.
  3. Navigate to "Build Phases".
  4. Expand the "Copy Bundle Resources".
  5. Press the "+" button and in the dialog that opens, find your some.data file, and click "Add".

Access the file in code

Once your file is available in the bundle, you can access it wherever you need it, whether that's a test, some UI code, or somewhere else.

You can get the URL to your file as follows:

Bundle.main.url(forResource: "some", withExtension: "data", subdirectory: nil)

Once you have the URL, you access it however you want. For example, in my test app, some.data is a basic text file so I use to print the value of a Text view.

struct ContentView: View {
    private var contentsOfFile: String? {
        guard let url = Bundle.main.url(forResource: "some", withExtension: "data", subdirectory: nil) else {
            return nil
        }
		
        return try? String(contentsOf: url)
    }

    var body: some View {
        Text(contentsOfFile ?? "Unable to read file")
            .padding()
    }
}
  • Thank you so much. I can now read my sqlite database now.

Add a Comment

Replies

It's worth bearing in mind that your file structure for development is different to the file structure for the compiled application. some.data may not be at the same level as ContentView.swift, or even available to your application by default.

The best way to achieve what you need is to setup Xcode to copy the some.data file into your bundle as a resource, which you can then access at runtime.

Add the file to the application bundle

  1. In Xcode, select your Project in the project navigator.
  2. Find the application target for your project in the Targets list. This will usually have a little app icon.
  3. Navigate to "Build Phases".
  4. Expand the "Copy Bundle Resources".
  5. Press the "+" button and in the dialog that opens, find your some.data file, and click "Add".

Add the file to the test bundle

The steps are very similar for adding it to the testing target.

  1. In Xcode, select your Project in the project navigator.
  2. Find the test target for your project in the Targets list. This will usually either have a little icon with four squares, or a little with two squares and a slider, depending on whether you are doing UI tests or code tests.
  3. Navigate to "Build Phases".
  4. Expand the "Copy Bundle Resources".
  5. Press the "+" button and in the dialog that opens, find your some.data file, and click "Add".

Access the file in code

Once your file is available in the bundle, you can access it wherever you need it, whether that's a test, some UI code, or somewhere else.

You can get the URL to your file as follows:

Bundle.main.url(forResource: "some", withExtension: "data", subdirectory: nil)

Once you have the URL, you access it however you want. For example, in my test app, some.data is a basic text file so I use to print the value of a Text view.

struct ContentView: View {
    private var contentsOfFile: String? {
        guard let url = Bundle.main.url(forResource: "some", withExtension: "data", subdirectory: nil) else {
            return nil
        }
		
        return try? String(contentsOf: url)
    }

    var body: some View {
        Text(contentsOfFile ?? "Unable to read file")
            .padding()
    }
}
  • Thank you so much. I can now read my sqlite database now.

Add a Comment