我有一个像这样的字典数组:
创建词典:
let dateOfSurvey = String(NSDate())
let idOfSurvey = General.codeClasse + "-" + String(General.codePersonnel)
let dictionaryResults = ["dateOfSurvey": dateOfSurvey,
"ID": idOfSurvey,
"themaQuestionStep": questionResultThema,
"actionQuestionStep": questionResultAction,
"F01D1a": questionResult01,
"F02D2c": questionResult02,
"F03D3a": questionResult03,
"F04D4a": questionResult04,
"F05D1b": questionResult05,
"F06D2b": questionResult06,
"F07D3b": questionResult07,
"F08D4b": questionResult08,
"F09D1c": questionResult09,
"F10D2a": questionResult10,
"F11D3c": questionResult11,
"F12D4c": questionResult12]
然后添加到数组中:
arrayOfResults.addObject(dictionaryResults)
我想通过邮件发送/导出结果,并认为CSV文件是一个好方法。
有没有一种简单的方法可以在swift中的字典数组中创建这个csv?
答案 0 :(得分:1)
在没有dict数组的解决方案后,我必须更改数组列表中的数据 dictionaryResults 成为 arrayOfSurvey
let dateOfSurvey = String(NSDate())
let idOfSurvey = General.codeClasse + "-" + String(General.codePersonnel)
let arrayOfSurvey = [dateOfSurvey , idOfSurvey , questionResultThema , questionResultAction, questionResult01, questionResult02, questionResult03, questionResult04, questionResult05, questionResult06, questionResult07, questionResult08, questionResult09, questionResult10, questionResult11, questionResult12]
我丢失了键值,但现在能够将数据放入cvs
arrayOfResults.addObject(arrayOfSurvey)
let count = arrayOfResults.count
General.cvs = "dateOfSurvey" + "," + "ID" + "," + "themaQuestionStep" + "," + "actionQuestionStep" + "," + "F01D1a" + "," + "F02D2c" + "," + "F03D3a" + "," + "F04D4a" + "," + "F05D1b" + "," + "F06D2b" + "," + "F07D3b" + "," + "F08D4b" + "," + "F09D1c" + "," + "F10D2a" + "," + "F11D3c" + "," + "F12D4c"
for i in 0...count {
let arrayAtIndex = arrayOfResults.objectAtIndex(i)
let stringRepresentation = arrayAtIndex.componentsJoinedByString(",")
General.cvs += "\n" + stringRepresentation
}
let fileData = General.cvs.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)