Load files in xcode unit tests

前端 未结 7 1892
失恋的感觉
失恋的感觉 2021-02-02 05:17

I have an Xcode 5 unit test project and some test xml files related to it. I\'ve tried a bunch of approaches but I can\'t seem to load the xml files.

I\'ve tried the fol

7条回答
  •  被撕碎了的回忆
    2021-02-02 05:40

    With swift Swift 3 the syntax self.dynamicType has been deprecated, use this instead

    let testBundle = Bundle(for: type(of: self))
    guard let ressourceURL = testBundle.url(forResource: "TestData", ofType: "xml") else {
        // file does not exist
        return
    }
    do {
        let ressourceData = try Data(contentsOf: ressourceURL)
    } catch let error {
        // some error occurred when reading the file
    }
    

    or

    guard let ressourceURL = testBundle.url(forResource: "TestData", withExtension: "xml")
    

提交回复
热议问题