title: swift 3.1 how to get array or dictionary from CSV
body: <p>How could I use data in this kind of CSV file? Or how could I print for example row 2 value for "inside" column and assign it to a property / entity?</p>
  
  <p>I have this kind of file I got from excel file converted to Numbers, I'd like to grab data for each column and use them.</p>
  
  <p>The original CSV file opened in numbers:</p>
  
  <p><a href="https://i.stack.imgur.com/Mx6AI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Mx6AI.png" alt="enter image description here"></a></p>
  
  <p>The console output I got:</p>
  
  <p><a href="https://i.stack.imgur.com/tJWjK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/tJWjK.png" alt="enter image description here"></a></p>
  
  <p>Using this methods:</p>
  
  <pre><code>func readDataFromCSV(fileName:String, fileType: String)-&gt; String!{
          guard let filepath = Bundle.main.path(forResource: fileName, ofType: fileType)
              else {
                  return nil
          }
          do {
              var contents = try String(contentsOfFile: filepath, encoding: .utf8)
              contents = cleanRows(file: contents)
              return contents
          } catch {
              print("File Read Error for file \(filepath)")
              return nil
          }
      }
  
  
  func cleanRows(file:String)-&gt;String{
      var cleanFile = file
      cleanFile = cleanFile.replacingOccurrences(of: "\r", with: "\n")
      cleanFile = cleanFile.replacingOccurrences(of: "\n\n", with: "\n")
      //        cleanFile = cleanFile.replacingOccurrences(of: ";;", with: "")
      //        cleanFile = cleanFile.replacingOccurrences(of: ";\n", with: "")
      return cleanFile
  }
  </code></pre>
  
  <p>SOLUTION thanks to Jens Meder</p>
  
  <p>using </p>
  
  <pre><code> func csv(data: String) -&gt; [[String]] {
          var result: [[String]] = []
          let rows = data.components(separatedBy: "\n")
          for row in rows {
              let columns = row.components(separatedBy: ";")
              result.append(columns)
          }
          return result
      }
  </code></pre>
  
  <p>in viewDidLoad</p>
  
  <pre><code>var data = readDataFromCSV(fileName: kCSVFileName, fileType: kCSVFileExtension)
      data = cleanRows(file: data!)
      let csvRows = csv(data: data!)
      print(csvRows[1][1]) // UXM n. 166/167
  </code></pre>