【发布时间】:2016-08-03 11:52:52
【问题描述】:
我曾经使用以下代码从我的应用程序包中读取文本文件。但是,无论我的应用程序如何,都找不到它们了。我 100% 确定我的所有文件都在 Assets.xcassets 中,我可以看到它们、编辑它们、将它们从一个目录转换到另一个目录。但是我的应用程序不想阅读它们,请告诉我我错过了什么!
这是我正在使用的程序...
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)
【问题讨论】:
-
我不确定将这些文本文件存储为资产是否可行。对于不是艺术品的各种捆绑资源,我会坚持使用@Pyro 的答案。
标签: swift file text bundle nsfilemanager