How do I load a text file line by line into an array with Swift?

前端 未结 9 714
盖世英雄少女心
盖世英雄少女心 2020-12-29 18:51

How do I load a text file line by line into an array with swift?

9条回答
  •  有刺的猬
    2020-12-29 19:52

    If you are in Swift 2.0, you should use:

    let path = NSBundle.mainBundle().pathForResource(fileName, ofType: nil)
    if path == nil {
      return nil
    }
    
    var fileContents: String? = nil
    do {
      fileContents = try String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
    } catch _ as NSError {
      return nil
    }
    

提交回复
热议问题