在我的swift项目的Tests目录中,我有一个用于存放测试用例的目录和一个用于存放测试文件的目录。
+ Tests
+ UnitTest
+ MySwiftTests.swift
+ SourceFiles
+ file1.xml我需要创建一个FileManager来加载我的MySwiftTests中的'file1.xml‘。我的问题是如何在FileManager的url中指定与测试用例相关的SearchPathDirectory和SearchPathDomainMask?
函数URL (用于: FileManager.SearchPathDirectory,位于: FileManager.SearchPathDomainMask,appropriateFor: URL?,创建: Bool) -> URL
https://developer.apple.com/documentation/foundation/filemanager
发布于 2020-07-22 15:50:05
let bundle = Bundle(for: type(of: self))
guard let path = bundle.path(forResource: "file1", ofType: "xml") else {
// File not found ... oops
return
}
// Now you can access the file using e.g. String(contentsOfFile:)
let string = try? String(contentsOfFile: path)
// or Data? using the FileManager
let data = FileManager.default.contents(atPath: path)确保将file1.xml添加到测试目标中
https://stackoverflow.com/questions/63028452
复制相似问题