簡體   English   中英

Xcode 8.3.3 中的 iOS Swift Researchkit Predicate 問題

[英]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] 無法將類型“(謂詞:__ObjC.NSPredicate,destinationStepIdentifier:Swift.String)”(0x15fe50570)的值強制轉換為:“__NSPredicate,目的地步長預測,”__NSPredicate : 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 數組中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM