亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從文本文件中讀取和寫入字符串

從文本文件中讀取和寫入字符串

肥皂起泡泡 2019-06-13 17:24:31
從文本文件中讀取和寫入字符串我需要將數據讀寫到文本文件/從文本文件中寫入數據,但我一直無法弄清楚如何讀取/從文本文件中寫入數據。我在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)}


查看完整回答
反對 回復 2019-06-13
?
MYYA

TA貢獻1868條經驗 獲得超4個贊

假設您移動了文本文件data.txt對于您的Xcode-項目(使用拖放并檢查“復制文件(如果必要的話)”),您可以執行以下操作,就像在Object-C中一樣:

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

最新情況: 
要從Bundle(IOS)讀取文件,可以使用:

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)!


查看完整回答
反對 回復 2019-06-13
  • 3 回答
  • 0 關注
  • 1653 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號