从文本文件中读取和写入字符串我需要将数据读写到文本文件/从文本文件中写入数据,但我一直无法弄清楚如何读取/从文本文件中写入数据。我在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 关注
- 1615 浏览
添加回答
举报
0/150
提交
取消