无法找到在ORKStepResult数组中将简单答案作为字符串的逻辑或解决方案。 它之前正在努力:
select number/total AS percent
from (
your query goes here
) derivedTable
经过一段时间和更新(swift& researchkit),它没有。 这条线让我抓狂:
for stepResults in taskViewController.result.results! as! [ORKStepResult] {
for result in stepResults.results! {
switch result.identifier {
case "themaQuestionStep":
if let questionResult = result as? ORKQuestionResult {
questionResultThema = String(questionResult.answer?.objectAtIndex(0))
}
如果我这样做:
questionResultThema = String(questionResult.answer?.objectAtIndex(0))
它给了我正确的答案,但是在一个数组中:
questionResultThema = String(describing: questionResult.answer)
答案 0 :(得分:1)
最好将结果对象转换为您期望的特定问题结果类型。这将使您可以访问各种子类的类型安全和更方便的属性。例如,如果您希望它是ORKTextQuestionResult:
if let questionResult = result as? ORKTextQuestionResult {
questionResultThema = questionResult.textAnswer
}
另一个例子(猜测为什么questionResult.answer
是一个数组)。对于ORKChoiceQuestionResult,您可以执行以下操作:
if let questionResult = result as? ORKChoiceQuestionResult,
let answer = questionResult.choiceAnswers.first as? String {
questionResultThema = answer
}