iOS Swift Xcode 8.3.3 中的 Researchkit 谓词问题
iOS Swift Researchkit Predicate issue in Xcode 8.3.3
我正在使用基于 Researchkit 的应用程序,我正在根据 Xcode 8.3.3 中单选题的选定选项导航到问题。不幸的是,应用程序在设置导航规则时动态崩溃。在 Xcode 8.2.1 的早些时候,我没有遇到任何问题并且工作顺利。请让我知道我的代码出了什么问题,以下是崩溃日志:
无法将类型“(谓词:__ObjC.NSPredicate,destinationStepIdentifier:Swift.String)”(0x15fe50570)类型的值转换为“(resultPredicate:__ObjC.NSPredicate,destinationStepIdentifier:Swift.String)' (0x15fe60530)。
2017-10-10 07:37:57.530808 Turbo[7440:2981376] 无法将类型'(predicate : __ObjC.NSPredicate, destinationStepIdentifier : Swift.String)' (0x15fe50570) 的值转换为'(resultPredicate : __ObjC.NSPredicate, destinationStepIdentifier : Swift.String)' (0x15fe60530).
//Question0
let textChoiceOneText = NSLocalizedString("Choice 1", comment: "")
let textChoiceTwoText = NSLocalizedString("Choice 2", comment: "")
let textChoiceThreeText = NSLocalizedString("Choice 3", comment: "")
// The text to display can be separate from the value coded for each choice:
let textChoices = [
ORKTextChoice(text: textChoiceOneText, value: "choice_1" as NSCoding & NSCopying & NSObjectProtocol),
ORKTextChoice(text: textChoiceTwoText, value: "choice_2" as NSCoding & NSCopying & NSObjectProtocol),
ORKTextChoice(text: textChoiceThreeText, value: "choice_3" as NSCoding & NSCopying & NSObjectProtocol)
]
let answerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: textChoices)
let questionStepzero = ORKQuestionStep(identifier: String(describing:"singlechoice0"), title: "titel", answer: answerFormat)
//question1
let question1 = ORKQuestionStep(identifier: "question1")
question1.answerFormat = ORKBooleanAnswerFormat()
//question2
let question2 = ORKQuestionStep(identifier: "question2")
question2.answerFormat = ORKBooleanAnswerFormat()
//Question6
let question6 = ORKQuestionStep(identifier: "question6")
question6.answerFormat = ORKBooleanAnswerFormat()
//Question 7
let defaultDate = Date()
let minDate = Date()
let maxDate = Date()
let nameQuestionStepTitle = "title message"
let dateAnswer = ORKDateAnswerFormat(style:ORKDateAnswerStyle.date, defaultDate: defaultDate, minimumDate: minDate, maximumDate: maxDate, calendar: nil)
let dataPickerQuestionStep7 = ORKQuestionStep(identifier: "datequestion7", title:nameQuestionStepTitle, answer: dateAnswer)
let steps = [questionStepzero,question1, question2,question6,dataPickerQuestionStep7]
let task = ORKNavigableOrderedTask(identifier: "task", steps: steps)
let predicate1 = ORKResultPredicate.predicateForChoiceQuestionResult(with: ORKResultSelector(resultIdentifier: "singlechoice0"), matchingPattern: "choice_1")
let predicate2 = ORKResultPredicate.predicateForChoiceQuestionResult(with: ORKResultSelector(resultIdentifier: "singlechoice0"), matchingPattern: "choice_2")
let predicate3 = ORKResultPredicate.predicateForChoiceQuestionResult(with: ORKResultSelector(resultIdentifier: "singlechoice0"), matchingPattern: "choice_3")
let singleChulesArray:NSMutableArray = NSMutableArray()
var dict:NSMutableDictionary = NSMutableDictionary()
dict.setObject(predicate1, forKey: "predicateInstance" as NSCopying)
dict.setObject("datequestion7", forKey: "Destination" as NSCopying)
singleChulesArray.add(dict)
dict = NSMutableDictionary()
dict.setObject(predicate2, forKey: "predicateInstance" as NSCopying)
dict.setObject("question2", forKey: "Destination" as NSCopying)
singleChulesArray.add(dict)
dict = NSMutableDictionary()
dict.setObject(predicate3, forKey: "predicateInstance" as NSCopying)
dict.setObject("question6", forKey: "Destination" as NSCopying)
singleChulesArray.add(dict)
//Static loading of Predicates and Destintionidentifiers
/*
let predicateRule1 = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: [
(resultPredicate: predicate1, destinationStepIdentifier: "datequestion7"),
(resultPredicate: predicate2, destinationStepIdentifier: "question2"),
(resultPredicate: predicate3, destinationStepIdentifier: "question6")
])
*/
print("singleChulesArray",singleChulesArray)
//Dynamic loading of Predicates and Destination identifiers
var stuff:[(predicate: NSPredicate, destinationStepIdentifier: String)] = [(predicate: NSPredicate, destinationStepIdentifier: String)]()
for (_, PredicateDict) in singleChulesArray.enumerated()
{
stuff += [(predicate: (PredicateDict as AnyObject).value(forKey: "predicateInstance") as! NSPredicate, destinationStepIdentifier: (PredicateDict as AnyObject).value(forKey: "Destination") as! String)]
}
let predicateRule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: stuff as! [(resultPredicate: NSPredicate, destinationStepIdentifier: String)])
task.setNavigationRule(predicateRule, forTriggerStepIdentifier: "singlechoice0")
let taskViewController = ORKTaskViewController(task: task, taskRun: nil)
taskViewController.view.tintColor = TurboConstants.globalAccess.primaryClr
taskViewController.delegate = self
present(taskViewController, animated: true, completion: nil)
您没有正确使用 ORKPredicateStepNavigationRule
初始值设定项。初始化程序将元组数组作为参数。您需要在初始化程序中传递元组的元素名称:
let predicateRule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: [(resultPredicate: predicate1, destinationStepIdentifier: "datequestion7")])
或多个元组:
let predicateRule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: [
(resultPredicate: predicate1, destinationStepIdentifier: "datequestion7"),
(resultPredicate: predicate2, destinationStepIdentifier: "question2"),
(resultPredicate: predicate3, destinationStepIdentifier: "question6"),
])
或动态地(如您上面的代码):
var stuff = [(resultPredicate: NSPredicate, destinationStepIdentifier: String)]()
for predicateDict in singleChulesArray
{
guard
let predicateDict = predicateDict as? [String: Any],
let predicate = predicateDict["predicateInstance"] as? NSPredicate,
let identifier = predicateDict["Destination"] as? String
else { continue }
stuff.append((resultPredicate: predicate, destinationStepIdentifier: identifier))
}
要消除崩溃,您只需将元组元素名称 predicate
更改为正确的名称 resultPredicate
。当元组用作方法中的参数时,元素名称的命名必须与方法签名中的完全相同。否则你会崩溃。
我冒昧地对您的那部分代码进行了更多更改。我删除了强制转换(这几乎从来都不是一个好主意)并改用可选的展开。我还使用 append
将一个新元组添加到 stuff
数组,而不是创建一个新数组(将元组作为其唯一元素),然后将该数组添加到 stuff 数组。
我正在使用基于 Researchkit 的应用程序,我正在根据 Xcode 8.3.3 中单选题的选定选项导航到问题。不幸的是,应用程序在设置导航规则时动态崩溃。在 Xcode 8.2.1 的早些时候,我没有遇到任何问题并且工作顺利。请让我知道我的代码出了什么问题,以下是崩溃日志:
无法将类型“(谓词:__ObjC.NSPredicate,destinationStepIdentifier:Swift.String)”(0x15fe50570)类型的值转换为“(resultPredicate:__ObjC.NSPredicate,destinationStepIdentifier:Swift.String)' (0x15fe60530)。 2017-10-10 07:37:57.530808 Turbo[7440:2981376] 无法将类型'(predicate : __ObjC.NSPredicate, destinationStepIdentifier : Swift.String)' (0x15fe50570) 的值转换为'(resultPredicate : __ObjC.NSPredicate, destinationStepIdentifier : Swift.String)' (0x15fe60530).
//Question0
let textChoiceOneText = NSLocalizedString("Choice 1", comment: "")
let textChoiceTwoText = NSLocalizedString("Choice 2", comment: "")
let textChoiceThreeText = NSLocalizedString("Choice 3", comment: "")
// The text to display can be separate from the value coded for each choice:
let textChoices = [
ORKTextChoice(text: textChoiceOneText, value: "choice_1" as NSCoding & NSCopying & NSObjectProtocol),
ORKTextChoice(text: textChoiceTwoText, value: "choice_2" as NSCoding & NSCopying & NSObjectProtocol),
ORKTextChoice(text: textChoiceThreeText, value: "choice_3" as NSCoding & NSCopying & NSObjectProtocol)
]
let answerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: textChoices)
let questionStepzero = ORKQuestionStep(identifier: String(describing:"singlechoice0"), title: "titel", answer: answerFormat)
//question1
let question1 = ORKQuestionStep(identifier: "question1")
question1.answerFormat = ORKBooleanAnswerFormat()
//question2
let question2 = ORKQuestionStep(identifier: "question2")
question2.answerFormat = ORKBooleanAnswerFormat()
//Question6
let question6 = ORKQuestionStep(identifier: "question6")
question6.answerFormat = ORKBooleanAnswerFormat()
//Question 7
let defaultDate = Date()
let minDate = Date()
let maxDate = Date()
let nameQuestionStepTitle = "title message"
let dateAnswer = ORKDateAnswerFormat(style:ORKDateAnswerStyle.date, defaultDate: defaultDate, minimumDate: minDate, maximumDate: maxDate, calendar: nil)
let dataPickerQuestionStep7 = ORKQuestionStep(identifier: "datequestion7", title:nameQuestionStepTitle, answer: dateAnswer)
let steps = [questionStepzero,question1, question2,question6,dataPickerQuestionStep7]
let task = ORKNavigableOrderedTask(identifier: "task", steps: steps)
let predicate1 = ORKResultPredicate.predicateForChoiceQuestionResult(with: ORKResultSelector(resultIdentifier: "singlechoice0"), matchingPattern: "choice_1")
let predicate2 = ORKResultPredicate.predicateForChoiceQuestionResult(with: ORKResultSelector(resultIdentifier: "singlechoice0"), matchingPattern: "choice_2")
let predicate3 = ORKResultPredicate.predicateForChoiceQuestionResult(with: ORKResultSelector(resultIdentifier: "singlechoice0"), matchingPattern: "choice_3")
let singleChulesArray:NSMutableArray = NSMutableArray()
var dict:NSMutableDictionary = NSMutableDictionary()
dict.setObject(predicate1, forKey: "predicateInstance" as NSCopying)
dict.setObject("datequestion7", forKey: "Destination" as NSCopying)
singleChulesArray.add(dict)
dict = NSMutableDictionary()
dict.setObject(predicate2, forKey: "predicateInstance" as NSCopying)
dict.setObject("question2", forKey: "Destination" as NSCopying)
singleChulesArray.add(dict)
dict = NSMutableDictionary()
dict.setObject(predicate3, forKey: "predicateInstance" as NSCopying)
dict.setObject("question6", forKey: "Destination" as NSCopying)
singleChulesArray.add(dict)
//Static loading of Predicates and Destintionidentifiers
/*
let predicateRule1 = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: [
(resultPredicate: predicate1, destinationStepIdentifier: "datequestion7"),
(resultPredicate: predicate2, destinationStepIdentifier: "question2"),
(resultPredicate: predicate3, destinationStepIdentifier: "question6")
])
*/
print("singleChulesArray",singleChulesArray)
//Dynamic loading of Predicates and Destination identifiers
var stuff:[(predicate: NSPredicate, destinationStepIdentifier: String)] = [(predicate: NSPredicate, destinationStepIdentifier: String)]()
for (_, PredicateDict) in singleChulesArray.enumerated()
{
stuff += [(predicate: (PredicateDict as AnyObject).value(forKey: "predicateInstance") as! NSPredicate, destinationStepIdentifier: (PredicateDict as AnyObject).value(forKey: "Destination") as! String)]
}
let predicateRule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: stuff as! [(resultPredicate: NSPredicate, destinationStepIdentifier: String)])
task.setNavigationRule(predicateRule, forTriggerStepIdentifier: "singlechoice0")
let taskViewController = ORKTaskViewController(task: task, taskRun: nil)
taskViewController.view.tintColor = TurboConstants.globalAccess.primaryClr
taskViewController.delegate = self
present(taskViewController, animated: true, completion: nil)
您没有正确使用 ORKPredicateStepNavigationRule
初始值设定项。初始化程序将元组数组作为参数。您需要在初始化程序中传递元组的元素名称:
let predicateRule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: [(resultPredicate: predicate1, destinationStepIdentifier: "datequestion7")])
或多个元组:
let predicateRule = ORKPredicateStepNavigationRule(resultPredicatesAndDestinationStepIdentifiers: [
(resultPredicate: predicate1, destinationStepIdentifier: "datequestion7"),
(resultPredicate: predicate2, destinationStepIdentifier: "question2"),
(resultPredicate: predicate3, destinationStepIdentifier: "question6"),
])
或动态地(如您上面的代码):
var stuff = [(resultPredicate: NSPredicate, destinationStepIdentifier: String)]()
for predicateDict in singleChulesArray
{
guard
let predicateDict = predicateDict as? [String: Any],
let predicate = predicateDict["predicateInstance"] as? NSPredicate,
let identifier = predicateDict["Destination"] as? String
else { continue }
stuff.append((resultPredicate: predicate, destinationStepIdentifier: identifier))
}
要消除崩溃,您只需将元组元素名称 predicate
更改为正确的名称 resultPredicate
。当元组用作方法中的参数时,元素名称的命名必须与方法签名中的完全相同。否则你会崩溃。
我冒昧地对您的那部分代码进行了更多更改。我删除了强制转换(这几乎从来都不是一个好主意)并改用可选的展开。我还使用 append
将一个新元组添加到 stuff
数组,而不是创建一个新数组(将元组作为其唯一元素),然后将该数组添加到 stuff 数组。