Read and write a String from text file

前端 未结 21 1426
别跟我提以往
别跟我提以往 2020-11-22 00:02

I need to read and write data to/from a text file, but I haven\'t been able to figure out how.

I found this sample code in the Swift\'s iBook, but I still don\'t kno

21条回答
  •  鱼传尺愫
    2020-11-22 00:51

    I want to show you only the first part, that is read. Here's how simply you can read:

    Swift 3:

    let s = try String(contentsOfFile: Bundle.main.path(forResource: "myFile", ofType: "txt")!)
    

    Swift 2:

    let s = try! String(contentsOfFile: NSBundle.mainBundle().pathForResource("myFile", ofType: "txt")!)
    

提交回复
热议问题