Scanning CSV with text fields into Array Swift -


i have csv scanning method found here. answer led me there here.

my issue is: csv data trying scan contains commas (in field called "notes" others). presents problem when scan csv, because interprets notes field having seperated values in it.

can me edit method handle commas inside of text field? unsure of how save csv delimit field. not sure exact changes need made in method , in save options csv.

here function straight app:

func scancsv () -> array<dictionary<string,string>> {     var mycsvcontents = array<dictionary<string, string>>()      //rawdata global string variable gets set csv string on object init.     let allrows = rawdata.componentsseparatedbystring("\n")     let headers = allrows[0].componentsseparatedbystring(",")      runfunctiononrowsfromfile(headers, withfunction: {         (arow: dictionary<string, string>) in         mycsvcontents.append(arow)     })      return mycsvcontents  } func runfunctiononrowsfromfile(thecolumnnames:array<string>, withfunction thefunction:(dictionary<string, string>)->()) {      var fileobjectcleaned = rawdata.stringbyreplacingoccurrencesofstring("\r", withstring: "\n")      fileobjectcleaned = fileobjectcleaned.stringbyreplacingoccurrencesofstring("\n\n", withstring: "\n")      let objectarray = fileobjectcleaned.componentsseparatedbystring("\n")      anobjectrow in objectarray {          let objectcolumns = anobjectrow.componentsseparatedbystring(",")          var adictionaryentry = dictionary<string, string>()          var columnindex = 0          anobjectcolumn in objectcolumns {              adictionaryentry[thecolumnnames[columnindex]] = anobjectcolumn.stringbyreplacingoccurrencesofstring("\"", withstring: "", options: nsstringcompareoptions.caseinsensitivesearch, range: nil)              columnindex++         }          if adictionaryentry.count>1{             thefunction(adictionaryentry)         }     } } 

i realize question has been asked before, getting myself confused syntax differences other languages have in answers. new programmer, if isn't brutally obvious.

generally, when dealing commonly used formats such csv, preferable find , use existing library. there exist couple in swift, such cswiftv , swiftcsv (the former of adheres rfc4180 standard).

the reason of details in common tasks (whatever task may be) going @ least bit tricky right, using existing library instead of writing yourself, can minimize number of people need right.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -