मैं लाइन द्वारा एक टेक्स्ट फ़ाइल लाइन पढ़ना चाहता हूं और इसे आईओएस स्क्रीन पर प्रदर्शित कर रहा हूं उदाहरण यहां दिखाया गया है.
बनाना textView.text
वैकल्पिक एकमात्र तरीका था जिसे मैं प्राप्त कर सकता था readDataFromFile
चलाने के लिए। जब मैं क्लिक करता हूं load
समारोह चलता है लेकिन हमेशा वापस आता है nil
। मुझे लगता है कि फ़ाइल नहीं मिली है।
परीक्षण उद्देश्यों के लिए मैंने टेक्स्ट फ़ाइल बनाई हैXcode। मैंने इसे डेस्कटॉप पर और साथ ही प्रोजेक्ट फ़ोल्डर में सहेजने का भी प्रयास किया। किसी भी तरह से यह परियोजना नेविगेटर से पठनीय था। मैंने TextEdit का उपयोग करके फ़ाइल बनाने का भी प्रयास किया क्योंकि ऐप को अंततः एक्सकोड के बाहर बनाई गई टेक्स्ट फ़ाइलों को पढ़ने की आवश्यकता है।
मैं आभारी हूं कि कोई व्यक्ति यह समझा सकता है कि टेक्स्ट फ़ाइल क्यों नहीं मिली है, चाहे परियोजना को खोजने के लिए मुझे कुछ और करने की ज़रूरत है या यदि फंक्शन फ़ंक्शन रिटर्न nil
जिस तरह से मैंने इसे लागू किया है, उसके कारण किसी अन्य कारण से। धन्यवाद।
संपादित करें (2)
प्रतिक्रिया के लिए धन्यवाद। जवाब में, मैंने चार मामूली कोड परिवर्तन किए हैं जो पाठ फ़ाइल सामग्री को टेक्स्ट व्यू पर लिखे जाने की अनुमति देते हैं। परिवर्तनों में शामिल हैं: फ़ाइल नाम से फ़ाइल एक्सटेंशन को हटाकर, फ़ाइल नामों की एक सरणी जोड़ना, लौटना 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
उत्तर:
उत्तर № 1 के लिए 71) आपको इस पंक्ति में गलती है:
var fileName = "textFile.txt"
होना चाहिए:
var fileName = "textFile"
2) जांचें कि आपकी फाइल लक्ष्य से जुड़ी है:
जवाब के लिए 3 № 2
आपको कक्षा बंडल मालिक को इस तरह जोड़ने पर विचार करना चाहिए:
Bundle(for: ViewController.self).path(forResource: "fileName", ofType: "txt")
अगर मैं सही था तो इसे स्विफ्ट 2.0 से लागू किया गया था।