Swift 我无法从我的应用程序读取文本文件';s束
我曾经使用以下代码从我的应用程序包中读取文本文件。但是,无论我的应用程序是什么,都无法再找到它们。我100%确信我的所有文件都在Assets.xcsets中,我可以看到它们,编辑它们,将它们从一个目录转换到另一个目录。但是我的申请不想读,请告诉我我错过了什么 这是我正在使用的程序Swift 我无法从我的应用程序读取文本文件';s束,swift,file,text,bundle,nsfilemanager,Swift,File,Text,Bundle,Nsfilemanager,我曾经使用以下代码从我的应用程序包中读取文本文件。但是,无论我的应用程序是什么,都无法再找到它们。我100%确信我的所有文件都在Assets.xcsets中,我可以看到它们,编辑它们,将它们从一个目录转换到另一个目录。但是我的申请不想读,请告诉我我错过了什么 这是我正在使用的程序 func readBundle(file:String) -> String { var res: String = "" if let path = NSBundle.mainBundle().
func readBundle(file:String) -> String
{
var res: String = ""
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "txt")
{
let fm = NSFileManager()
let exists = fm.fileExistsAtPath(path)
if(exists)
{
let c = fm.contentsAtPath(path)
res = NSString(data: c!, encoding: NSUTF8StringEncoding) as! String
}
}
return res
}
我是这样使用它的:
let res = readBundle("test")
print(res)
在XCAssets中存储非映像文件时,应使用NSDataAsset访问其内容
在另一个选项“XCAssets”中,您可以创建一个单独的文件夹/资源组,而不是项目结构中的图像,检查它们是否存在于项目主目标的“构建阶段”部分的“复制捆绑包资源”中 如果您添加这样的资源,您当前的代码应该按原样工作
func readBundle(file:String) -> String
{
var res: String = ""
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "txt")
{
//you should be able to get the path
//other code as you has written in the question
}
return res
}
我不确定将这些文本文件存储为资产是否是可行的方法。对于不是艺术品的所有捆绑资源,我都会坚持@Pyro的答案。我想要,但stackoverflow.com不允许我这么做,因为我是新来的
func readBundle(file:String) -> String
{
var res: String = ""
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "txt")
{
//you should be able to get the path
//other code as you has written in the question
}
return res
}