如何获取文件内容?
How to get the file content?
我有如下所示的 Obj-C 代码
NSURL * url = [[NSBundle mainBundle] URLForResource:@"MyFile" withExtension:@"txt"];
NSError * err = nil;
NSString * string = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];
此代码运行正常。我需要把它转换成Swift一个,所以我这样做了
func getContentOfUrl(by fileName: String) -> String? {
var result: String?
guard let pathToFile = Bundle.main.path(forResource: fileName, ofType: "txt")
else {
return nil
}
do {
result = try String(contentsOf: URL(string: pathToFile)!, encoding: String.Encoding.utf8)
}
catch let err{
}
return result
}
我在 catch
块中遇到错误
Attempt to get content of URL FAILED, error: Error Domain=NSCocoaErrorDomain Code=256 "The file “MyFile.txt” couldn’t be opened." UserInfo={NSURL=/private/var/containers/Bundle/Application/63FB01-F11-411-B93-6578DEF57B/MyApp.app/MyFile.txt}
我做错了什么?
这对我有用。
func getFile(named: String) -> String? {
guard let url = Bundle.main.url(forResource: named, withExtension: "txt") else { return nil }
return try? String(contentsOf: url, encoding: .utf8)
}
如果不存在,您需要确保资源文件确实存在。
您可以在操场上测试这样的小功能。将要使用的文件添加到 playground 的 Resources 文件夹中。我使用文件名“MyText.txt”执行此操作,效果很好。
除了您应该使用 returns 一个 URL
而不是已经提到的文件路径的方法之外,您在创建 URL 你自己。 URL(string:)
需要 url 格式的字符串 (file://...) 但您有一个本地路径,因此您应该使用 URL(fileURLWithPath:)
。所以这将使您现有的代码工作
result = try String(contentsOf: URL(fileURLWithPath: pathToFile), encoding: .utf8)
或者直接使用URL
func content(of fileName: String) -> String? {
Bundle.main.url(forResource: fileName, withExtension: "txt")
.flatMap { try? String.init(contentsOf: [=11=], encoding: .utf8) }
}
我有如下所示的 Obj-C 代码
NSURL * url = [[NSBundle mainBundle] URLForResource:@"MyFile" withExtension:@"txt"];
NSError * err = nil;
NSString * string = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];
此代码运行正常。我需要把它转换成Swift一个,所以我这样做了
func getContentOfUrl(by fileName: String) -> String? {
var result: String?
guard let pathToFile = Bundle.main.path(forResource: fileName, ofType: "txt")
else {
return nil
}
do {
result = try String(contentsOf: URL(string: pathToFile)!, encoding: String.Encoding.utf8)
}
catch let err{
}
return result
}
我在 catch
块中遇到错误
Attempt to get content of URL FAILED, error: Error Domain=NSCocoaErrorDomain Code=256 "The file “MyFile.txt” couldn’t be opened." UserInfo={NSURL=/private/var/containers/Bundle/Application/63FB01-F11-411-B93-6578DEF57B/MyApp.app/MyFile.txt}
我做错了什么?
这对我有用。
func getFile(named: String) -> String? {
guard let url = Bundle.main.url(forResource: named, withExtension: "txt") else { return nil }
return try? String(contentsOf: url, encoding: .utf8)
}
如果不存在,您需要确保资源文件确实存在。
您可以在操场上测试这样的小功能。将要使用的文件添加到 playground 的 Resources 文件夹中。我使用文件名“MyText.txt”执行此操作,效果很好。
除了您应该使用 returns 一个 URL
而不是已经提到的文件路径的方法之外,您在创建 URL 你自己。 URL(string:)
需要 url 格式的字符串 (file://...) 但您有一个本地路径,因此您应该使用 URL(fileURLWithPath:)
。所以这将使您现有的代码工作
result = try String(contentsOf: URL(fileURLWithPath: pathToFile), encoding: .utf8)
或者直接使用URL
func content(of fileName: String) -> String? {
Bundle.main.url(forResource: fileName, withExtension: "txt")
.flatMap { try? String.init(contentsOf: [=11=], encoding: .utf8) }
}