Swift Text File To Array of Strings

后端 未结 6 1368
逝去的感伤
逝去的感伤 2021-01-01 22:20

I was wondering what the simplest and cleanest to read a text file into an array of strings is in swift.

Text file:

line 1
line 2
line 3 
line 4
         


        
6条回答
  •  轮回少年
    2021-01-01 23:07

    First you must read the file:

    let text = String(contentsOfFile: someFile, encoding: NSUTF8StringEncoding, error: nil)
    

    Then you separate it by line using the componentsSeparatedByString method:

    let lines : [String] = text.componentsSeparatedByString("\n")
    

提交回复
热议问题