Swift字典包含值,但未在调试器控制台上打印

时间:2019-04-14 10:25:17

标签: ios arrays swift dictionary

我正在像这样的字典中创建和打印值

let answersToSubmit = SubmitAnswerModel() //main model
let realm = try! Realm()
let savedExamResponse = realm.object(ofType: SavedExamResponse.self, forPrimaryKey: id)
answersToSubmit.uniqid = savedExamResponse?.uniqueId

var answerListToSubmit =  [QuestionAnswersToSubmit]()

for item in (savedExamResponse?.questionAnswerList)! {
    let answerToSubmit = QuestionAnswersToSubmit()
    answerToSubmit.qid =  item.questionId
    answerToSubmit.value.append(item.selectedOption)            
    answerListToSubmit.append(answerToSubmit)

}

answersToSubmit.answers = answerListToSubmit
print("length %s ",answersToSubmit.answers.count)//this is printing 100
print("answers to submit %s",answersToSubmit)//this is printing only the first part

输出是这样的

length %s  100
answers to submit %s SubmitAnswerModel {
    uniqid = test-5cb2f6a2d81034.66536499;
    //the rest of the object needs to printed here, isn't it
}

所以我不太确定是否将数组追加到字典中。任何帮助,将不胜感激。谢谢

2 个答案:

答案 0 :(得分:1)

如果您想要更全面的日志输出,请尝试使用dump()函数:

dump(answersToSubmit)

哪个会向控制台输出更多信息。

答案 1 :(得分:0)

那是您的for语句中的预期输出。我不确定为什么您尝试追加的领域不会像这样处理数组。如果需要存储数组,则可能需要使用领域列表。

但是从您已经显示的代码来看,这应该可以工作。

 for item in (savedExamResponse?.questionAnswerList)! {
   let answerToSubmit = QuestionAnswersToSubmit()
   answerToSubmit.qid =  item.questionId
   answerToSubmit.value = item.selectedOption          
   answerListToSubmit = answerToSubmit

}