Xcode 10: Load the same .xml file in project target and unit test target











up vote
0
down vote

favorite












I try to load the same .xml file in project target and unit test target.



It seems to be a similar problem like this question: Xcode. Image resources added to a test target are not copied into the tests bundle



In project target, this code snipped worked fine. I just have to add the "xmlString.xml" file to "Copy Files" (see image 1).



func parseFile() {
let stringPath = Bundle.main.path(forResource: "xmlString", ofType: "xml")
let url = NSURL(fileURLWithPath: stringPath!)
...
}


Image1



If I run the code snipped in unit test target, I get an error because the .xml file can not be found. I tried to add the .xml file to the "Copy Bundle Resources" without any success (see image 2).



image 2



The only way to get it working is to use the absolute path



func parseFile() {
let stringPath: String? = "/Users/.../Documents/Git/.../.../.../.../xmlString.xml"
let url = NSURL(fileURLWithPath: stringPath!)
...
}


Is there a way to use the Bundle.main.path(forResource: "xmlString", ofType: "xml") function instead of the absolute path?



Thanks in advance!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I try to load the same .xml file in project target and unit test target.



    It seems to be a similar problem like this question: Xcode. Image resources added to a test target are not copied into the tests bundle



    In project target, this code snipped worked fine. I just have to add the "xmlString.xml" file to "Copy Files" (see image 1).



    func parseFile() {
    let stringPath = Bundle.main.path(forResource: "xmlString", ofType: "xml")
    let url = NSURL(fileURLWithPath: stringPath!)
    ...
    }


    Image1



    If I run the code snipped in unit test target, I get an error because the .xml file can not be found. I tried to add the .xml file to the "Copy Bundle Resources" without any success (see image 2).



    image 2



    The only way to get it working is to use the absolute path



    func parseFile() {
    let stringPath: String? = "/Users/.../Documents/Git/.../.../.../.../xmlString.xml"
    let url = NSURL(fileURLWithPath: stringPath!)
    ...
    }


    Is there a way to use the Bundle.main.path(forResource: "xmlString", ofType: "xml") function instead of the absolute path?



    Thanks in advance!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I try to load the same .xml file in project target and unit test target.



      It seems to be a similar problem like this question: Xcode. Image resources added to a test target are not copied into the tests bundle



      In project target, this code snipped worked fine. I just have to add the "xmlString.xml" file to "Copy Files" (see image 1).



      func parseFile() {
      let stringPath = Bundle.main.path(forResource: "xmlString", ofType: "xml")
      let url = NSURL(fileURLWithPath: stringPath!)
      ...
      }


      Image1



      If I run the code snipped in unit test target, I get an error because the .xml file can not be found. I tried to add the .xml file to the "Copy Bundle Resources" without any success (see image 2).



      image 2



      The only way to get it working is to use the absolute path



      func parseFile() {
      let stringPath: String? = "/Users/.../Documents/Git/.../.../.../.../xmlString.xml"
      let url = NSURL(fileURLWithPath: stringPath!)
      ...
      }


      Is there a way to use the Bundle.main.path(forResource: "xmlString", ofType: "xml") function instead of the absolute path?



      Thanks in advance!










      share|improve this question















      I try to load the same .xml file in project target and unit test target.



      It seems to be a similar problem like this question: Xcode. Image resources added to a test target are not copied into the tests bundle



      In project target, this code snipped worked fine. I just have to add the "xmlString.xml" file to "Copy Files" (see image 1).



      func parseFile() {
      let stringPath = Bundle.main.path(forResource: "xmlString", ofType: "xml")
      let url = NSURL(fileURLWithPath: stringPath!)
      ...
      }


      Image1



      If I run the code snipped in unit test target, I get an error because the .xml file can not be found. I tried to add the .xml file to the "Copy Bundle Resources" without any success (see image 2).



      image 2



      The only way to get it working is to use the absolute path



      func parseFile() {
      let stringPath: String? = "/Users/.../Documents/Git/.../.../.../.../xmlString.xml"
      let url = NSURL(fileURLWithPath: stringPath!)
      ...
      }


      Is there a way to use the Bundle.main.path(forResource: "xmlString", ofType: "xml") function instead of the absolute path?



      Thanks in advance!







      ios swift xcode macos unit-testing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked 2 days ago









      Passe

      139116




      139116
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          You can get the bundle for your project like this:



          let projectBundle = Bundle(for: AnyClassInMyProject.self)


          where you replace AnyClassInMyProject with the name of an actual class in your project.



          You can then get the path to your .xml file like this:



          let stringPath = projectBundle.path(forResource: "xmlString", ofType: "xml")





          share|improve this answer





















          • let projectBundle = NSBundle </Users/.../Library/Developer/Xcode/DerivedData/Pattern-chlgsqhxjngcwabmjgaujwwewapq/Build/Products/Debug/UnitTest.xctest> (loaded) what to do with this information?
            – Passe
            2 days ago










          • Did you replace AnyClassInMyProject with the name of an actual class in your project? Your main project, not your test project? If so, projectBundle will refer to the bundle for your main project, i.e. it will be the equivalent of calling Bundle.main in your main project.
            – Mike Taverne
            2 days ago










          • Yes, I replaced "AnyClassInMyProject" with my XmlParser class in main project. So it seems to be completely wrong right? But why does it work in main target and not in test target? What do I have to change?
            – Passe
            yesterday











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239259%2fxcode-10-load-the-same-xml-file-in-project-target-and-unit-test-target%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          You can get the bundle for your project like this:



          let projectBundle = Bundle(for: AnyClassInMyProject.self)


          where you replace AnyClassInMyProject with the name of an actual class in your project.



          You can then get the path to your .xml file like this:



          let stringPath = projectBundle.path(forResource: "xmlString", ofType: "xml")





          share|improve this answer





















          • let projectBundle = NSBundle </Users/.../Library/Developer/Xcode/DerivedData/Pattern-chlgsqhxjngcwabmjgaujwwewapq/Build/Products/Debug/UnitTest.xctest> (loaded) what to do with this information?
            – Passe
            2 days ago










          • Did you replace AnyClassInMyProject with the name of an actual class in your project? Your main project, not your test project? If so, projectBundle will refer to the bundle for your main project, i.e. it will be the equivalent of calling Bundle.main in your main project.
            – Mike Taverne
            2 days ago










          • Yes, I replaced "AnyClassInMyProject" with my XmlParser class in main project. So it seems to be completely wrong right? But why does it work in main target and not in test target? What do I have to change?
            – Passe
            yesterday















          up vote
          0
          down vote













          You can get the bundle for your project like this:



          let projectBundle = Bundle(for: AnyClassInMyProject.self)


          where you replace AnyClassInMyProject with the name of an actual class in your project.



          You can then get the path to your .xml file like this:



          let stringPath = projectBundle.path(forResource: "xmlString", ofType: "xml")





          share|improve this answer





















          • let projectBundle = NSBundle </Users/.../Library/Developer/Xcode/DerivedData/Pattern-chlgsqhxjngcwabmjgaujwwewapq/Build/Products/Debug/UnitTest.xctest> (loaded) what to do with this information?
            – Passe
            2 days ago










          • Did you replace AnyClassInMyProject with the name of an actual class in your project? Your main project, not your test project? If so, projectBundle will refer to the bundle for your main project, i.e. it will be the equivalent of calling Bundle.main in your main project.
            – Mike Taverne
            2 days ago










          • Yes, I replaced "AnyClassInMyProject" with my XmlParser class in main project. So it seems to be completely wrong right? But why does it work in main target and not in test target? What do I have to change?
            – Passe
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          You can get the bundle for your project like this:



          let projectBundle = Bundle(for: AnyClassInMyProject.self)


          where you replace AnyClassInMyProject with the name of an actual class in your project.



          You can then get the path to your .xml file like this:



          let stringPath = projectBundle.path(forResource: "xmlString", ofType: "xml")





          share|improve this answer












          You can get the bundle for your project like this:



          let projectBundle = Bundle(for: AnyClassInMyProject.self)


          where you replace AnyClassInMyProject with the name of an actual class in your project.



          You can then get the path to your .xml file like this:



          let stringPath = projectBundle.path(forResource: "xmlString", ofType: "xml")






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Mike Taverne

          5,51821938




          5,51821938












          • let projectBundle = NSBundle </Users/.../Library/Developer/Xcode/DerivedData/Pattern-chlgsqhxjngcwabmjgaujwwewapq/Build/Products/Debug/UnitTest.xctest> (loaded) what to do with this information?
            – Passe
            2 days ago










          • Did you replace AnyClassInMyProject with the name of an actual class in your project? Your main project, not your test project? If so, projectBundle will refer to the bundle for your main project, i.e. it will be the equivalent of calling Bundle.main in your main project.
            – Mike Taverne
            2 days ago










          • Yes, I replaced "AnyClassInMyProject" with my XmlParser class in main project. So it seems to be completely wrong right? But why does it work in main target and not in test target? What do I have to change?
            – Passe
            yesterday


















          • let projectBundle = NSBundle </Users/.../Library/Developer/Xcode/DerivedData/Pattern-chlgsqhxjngcwabmjgaujwwewapq/Build/Products/Debug/UnitTest.xctest> (loaded) what to do with this information?
            – Passe
            2 days ago










          • Did you replace AnyClassInMyProject with the name of an actual class in your project? Your main project, not your test project? If so, projectBundle will refer to the bundle for your main project, i.e. it will be the equivalent of calling Bundle.main in your main project.
            – Mike Taverne
            2 days ago










          • Yes, I replaced "AnyClassInMyProject" with my XmlParser class in main project. So it seems to be completely wrong right? But why does it work in main target and not in test target? What do I have to change?
            – Passe
            yesterday
















          let projectBundle = NSBundle </Users/.../Library/Developer/Xcode/DerivedData/Pattern-chlgsqhxjngcwabmjgaujwwewapq/Build/Products/Debug/UnitTest.xctest> (loaded) what to do with this information?
          – Passe
          2 days ago




          let projectBundle = NSBundle </Users/.../Library/Developer/Xcode/DerivedData/Pattern-chlgsqhxjngcwabmjgaujwwewapq/Build/Products/Debug/UnitTest.xctest> (loaded) what to do with this information?
          – Passe
          2 days ago












          Did you replace AnyClassInMyProject with the name of an actual class in your project? Your main project, not your test project? If so, projectBundle will refer to the bundle for your main project, i.e. it will be the equivalent of calling Bundle.main in your main project.
          – Mike Taverne
          2 days ago




          Did you replace AnyClassInMyProject with the name of an actual class in your project? Your main project, not your test project? If so, projectBundle will refer to the bundle for your main project, i.e. it will be the equivalent of calling Bundle.main in your main project.
          – Mike Taverne
          2 days ago












          Yes, I replaced "AnyClassInMyProject" with my XmlParser class in main project. So it seems to be completely wrong right? But why does it work in main target and not in test target? What do I have to change?
          – Passe
          yesterday




          Yes, I replaced "AnyClassInMyProject" with my XmlParser class in main project. So it seems to be completely wrong right? But why does it work in main target and not in test target? What do I have to change?
          – Passe
          yesterday


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239259%2fxcode-10-load-the-same-xml-file-in-project-target-and-unit-test-target%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python