swift 3.1 how to get array or dictionary from CSV

前端 未结 4 1301
孤独总比滥情好
孤独总比滥情好 2020-12-24 01:50

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?

I have this k

4条回答
  •  孤独总比滥情好
    2020-12-24 02:27

    
This is for CSV file for swift 4.2 at 20181208

      var dataArray : [String] = []
          if  let path = Bundle.main.path(yourfile: "svenskaidiom", ofType: "csv")  
          {
            dataArray = []
            let url = URL(fileURLWithPath: path)
            do {
                let data = try Data(contentsOf: url) 
                let dataEncoded = String(data: data, encoding: .utf8)
                if  let dataArr = dataEncoded?.components(separatedBy: "\r\n").map({ $0.components(separatedBy: ";") })
              {
                for line in dataArr
                {
                        dataArray?.append(line)
                }
            }
            }
            catch let jsonErr {
                print("\n Error read CSV file: \n ", jsonErr)
            }
            }
    

提交回复
热议问题