從文本文件中讀取和寫入字符串我需要將數據讀寫到文本文件/從文本文件中寫入數據,但我一直無法弄清楚如何讀取/從文本文件中寫入數據。我在SWIFT的iBook中找到了這個示例代碼,但我仍然不知道如何寫入或讀取數據。import Cocoaclass DataImporter{
/*
DataImporter is a class to import data from an external file.
The class is assumed to take a non-trivial amount of time to initialize.
*/
var fileName = "data.txt"
// the DataImporter class would provide data importing functionality here}class DataManager{
@lazy var importer = DataImporter()
var data = String[]()
// the DataManager class would provide data management functionality here}let manager = DataManager()manager.
data += "Some data"manager.data += "Some more data"// the DataImporter instance for the importer property has not yet been created”
println(manager.importer.fileName)// the DataImporter instance for the importer property has now been created
// prints "data.txt”var str = "Hello World in Swift Language."
3 回答
PIPIONE
TA貢獻1829條經驗 獲得超9個贊
SWIFT 3.x和SWIFT 4.0
let file = "file.txt" //this is the file. we will write to and read from itlet text = "some text"
//just a textif let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let fileURL = dir.appendingPathComponent(file)
//writing do {
try text.write(to: fileURL, atomically: false, encoding: .utf8)
}
catch {/* error handling here */}
//reading do {
let text2 = try String(contentsOf: fileURL, encoding: .utf8)
}
catch {/* error handling here */}}SWIFT 2.2
let file = "file.txt" //this is the file. we will write to and read from itlet text = "some text"
//just a textif let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomainMask.AllDomainsMask, true).first {
let path = NSURL(fileURLWithPath: dir).URLByAppendingPathComponent(file)
//writing do {
try text.writeToURL(path, atomically: false, encoding: NSUTF8StringEncoding)
}
catch {/* error handling here */}
//reading do {
let text2 = try NSString(contentsOfURL: path, encoding: NSUTF8StringEncoding)
}
catch {/* error handling here */}}SWIFT 1.x
let file = "file.txt"if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,
NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
let dir = dirs[0] //documents directory let path = dir.stringByAppendingPathComponent(file);
let text = "some text"
//writing text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding, error: nil);
//reading let text2 = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)}
MYYA
TA貢獻1868條經驗 獲得超4個贊
data.txt
let bundle = NSBundle.mainBundle()let path = bundle.pathForResource("data", ofType: "txt")
let content = NSString.stringWithContentsOfFile(path) as Stringprintln(content) // prints the content of data.txt最新情況:
let path = NSBundle.mainBundle().pathForResource("FileName", ofType: "txt")var text = String(contentsOfFile: path!, encoding:
NSUTF8StringEncoding, error: nil)!println(text)SWIFT 3更新:
let path = Bundle.main.path(forResource: "data", ofType: "txt") // file path for file "data.txt"var text = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
- 3 回答
- 0 關注
- 1666 瀏覽
添加回答
舉報
0/150
提交
取消
