![](/img/trans.png)
[英]Bundle.main.path(forResource:ofType:inDirectory:) returns nil
[英]why does Bundle.main.path(forResource: fileName, ofType: “txt”) always return nil?
我想逐行讀取一個文本文件,並使用此處顯示的示例將其顯示在 iOS屏幕上。
制作textView.text
可選的是我能得到的唯一途徑readDataFromFile
運行。 當我單擊load
該函數運行,但始終返回nil
。 我認為這意味着找不到該文件。
為了進行測試,我在Xcode中創建了文本文件。 我還嘗試將其保存在桌面以及項目文件夾中。 無論哪種方式,都可以從項目導航器中讀取它。 我還嘗試使用TextEdit創建文件,因為該應用最終需要讀取在Xcode外部創建的文本文件。
如果有人可以解釋為什么從未找到文本文件,是否需要做其他事情才能使項目找到它,或者由於我的方式而使read函數由於其他原因返回nil
,我將不勝感激已經實施了。 謝謝。
編輯 (2)
感謝您的反饋。 作為響應,我做了四個次要的代碼更改,以允許將文本文件內容寫入textView。 更改包括:從文件名中刪除文件擴展名,添加文件名數組,返回String
而不是String?
從readDataFromFile
並用代碼重寫UITextView
。 這解決了我所知道的問題。
這是修改后的代碼
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)
}
}
編輯 (1)
該文件在項目導航器中可見。 我認為這意味着它在捆綁包中。
這是我的原始代碼
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
您應該考慮像這樣添加類包所有者:
Bundle(for: ViewController.self).path(forResource: "fileName", ofType: "txt")
如果我是對的,這是從swift 2.0實現的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.