为什么 Bundle.main.path(forResource: fileName, ofType: “txt") 总是返回 nil? [英] why does Bundle.main.path(forResource: fileName, ofType: "txt") always return nil?
问题描述
我想使用
I want to read a text file line by line and display it on an iOS screen using the example shown here.
Making textView.text
optional was the only way I could get readDataFromFile
to run. When I click load
the function runs but always returns nil
. I assume this means the file is not found.
For testing purposes I created the text file in Xcode. I also tried saving it on the desktop as well as in the project folder. Either way it was readable from the project navigator. I also tried creating the file using TextEdit because the app ultimately needs to read text files created outside Xcode.
I’d be grateful if someone can explain why the text file is never found, whether there is something else I need to do in order for the project to find it or if the read function returns nil
for some other reason due to the way I have implemented it. Thanks.
EDIT (2)
Thanks for the feedback. In response, I’ve made four minor code changes that allow the text file contents to be written to textView. Changes include: removing the file extension from the filename, adding an array of file names, returning String
instead of String?
from readDataFromFile
and rewriting UITextView
in code. This has solved problems I am aware of.
Here's the revised code
import UIKit
class ViewController: UIViewController {
var textView = UITextView()
var arrayOfStrings: [String]?
var fileNameWithExtension = "textFile.txt"
let arrayOfFileNames = ["textFile1.txt", "textFile2.txt", "textFile3.txt", "textFile4.txt", "textFile5.txt"]
var fileName = String()
override func viewDidLoad() {
super.viewDidLoad()
// remove comment in the next statement to test files named in ArrayOfFileNames
// fileNameWithExtension = arrayOfFileNames[4]
fileName = fileNameWithExtension.replacingOccurrences(of: ".txt", with: "")
createTextView()
createButton()
}
func readDataFromFile(fileName: String) -> String {
if let path = Bundle.main.path(forResource: fileName, ofType: nil) {
print(fileName)
do {
let data = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
arrayOfStrings = data.components(separatedBy: .newlines)
textView.text = arrayOfStrings?.joined(separator: "\n")
} catch {
textView.text = "file contents could not be loaded"
}
} else {
print(Bundle.main.path(forResource: fileName, ofType: "txt") as Any)
textView.text = "\(fileName) could not be found"
}
return textView.text
}
func createButton () {
let button = UIButton();
button.setTitle(String("Load"), for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.frame = CGRect(x: 100, y: 10, width: 200, height: 100)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
}
func buttonAction(myButton: UIButton) {
textView.text = readDataFromFile(fileName: fileName)
print(textView.text as Any)
}
func createTextView () {
textView = UITextView(frame: CGRect(x: 20.0, y: 75.0, width: 340.0, height: 400.0))
textView.textAlignment = NSTextAlignment.left
textView.textColor = UIColor.blue
textView.backgroundColor = UIColor.white
self.view.addSubview(textView)
}
}
EDIT (1)
The file is visible in the project navigator. I will assume that means it is in the bundle.
Here is my original code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView?
var arrayOfStrings: [String]?
var fileName = "textFile.txt"
override func viewDidLoad() {
super.viewDidLoad()
createButton()
}
func readDataFromFile(fileName: String) -> String? {
if let path = Bundle.main.path(forResource: fileName, ofType: "txt") {
print(fileName)
do {
let data = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
arrayOfStrings = data.components(separatedBy: .newlines)
print(arrayOfStrings as Any)
textView?.text = arrayOfStrings?.joined(separator: "/n")
return textView?.text
} catch {
textView?.text = "file contents could not be loaded"
return textView?.text
}
} else {
print(Bundle.main.path(forResource: fileName, ofType: "txt") as Any)
textView?.text = "\(fileName) could not be found"
return nil
}
}
func createButton () {
let button = UIButton();
button.setTitle(String("Load"), for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.frame = CGRect(x: 100, y: 15, width: 200, height: 100)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
}
func buttonAction(myButton: UIButton) {
print("works")
textView?.text = readDataFromFile(fileName: fileName)
print(textView?.text as Any)
}
textFile.txt
Line 1
Line 2
Line 3
Line 4
Line 5
1) You have mistake in this line:
var fileName = "textFile.txt"
should be:
var fileName = "textFile"
2) Check is your file connected to target:
这篇关于为什么 Bundle.main.path(forResource: fileName, ofType: “txt") 总是返回 nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!