Swift Text File To Array of Strings

后端 未结 6 1334
逝去的感伤
逝去的感伤 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 22:44

    Swift 4:

    do {
        let contents = try String(contentsOfFile: file, encoding: String.Encoding.utf8)
        let lines : [String] = contents.components(separatedBy: "\n")    
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    

提交回复
热议问题