title: Count number of lines in a txt/log -file, swift
body: <p>I can't figure out how to count lines in a txt/log-file.</p>
  
  <pre><code>var n = 0
  var x = 0
  while x &lt; 1{
  let path = NSBundle.mainBundle().pathForResource("test)", ofType: "log")
  var text = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)!
  
  var textArr = text.componentsSeparatedByString("\r")
  var rad: String = textArr[n]
  n=n+1
  }
  </code></pre>
  
  <p>With this code I get an "fatal error: Array index out of range" at the end of the file (but correct number of lines in variable n). I don't want the error, I want to save the number of lines in a variable. How should I do this?</p>