必须解开可选类型的值以引用成员,但其为隐式解开的可选属性

时间:2019-09-22 13:39:44

标签: swift optional

我为什么要得到这个?它是一个隐式展开的可选属性。我认为我不应该收到此错误。我尝试清理构建文件夹并删除派生数据,然后关闭Xcode。什么都没用。

var questionsWrap: QuestionsWrapper

func userMadeSelection(_ responseId: Int,
                       _ myTheir: Question.Response.Selections.MyTheir,
                       _ choice: Question.Response.Selections.MyTheir.Choice) {
    guard let id = question.id else { return }
    questionsWrap.saveSelection(
        questionId: id,
        responseID: responseId,
        myTheir: myTheir,
       choice: choice
    ) { success in
        if !success {
           AlertViewController<Any>.alertFailure(
               message:  "We made a mistake and we weren't able to save your selection."
            )
        }
    }
    singleQuestionDataSource.observableQuestion.value = question
    print(#line, "userMadeSelectionImportance: \(responseId) \(myTheir) \(choice)")
    if choice.changedBetweenPolarAndNeutral(
        with: questionsWrap.question.theirInteractionStyle
        ) {
        let presenter = Question.Importance.Presenter(question)
        update(presenter?.importance)
        questionCell?.importancePresenter = presenter
    }
}

方法定义

func saveSelection(
        questionId: Int,
        responseID: Int,
        myTheir: Question.Response.Selections.MyTheir,
        choice: Question.Response.Selections.MyTheir.Choice,
        context: Context,
        successAction: SuccessAction? = nil
        ) {

        guard let questionIndex = questions.index(of: questionId),
            let responseIndex = questions[questionIndex].responses.index(of: responseID) else {
                successAction?(false)
                return
        }

        questions[questionIndex].responses[responseIndex].set(myTheir, choice, for: context)
        let response = questions[questionIndex].responses[responseIndex]
        URL.make(
           my: response.choice(for: .my, UserDefaults.questionContext),
           their: response.choice(for: .their, UserDefaults.questionContext),
           forResponseID: responseID,
           forQuestionID: questionId,
           forContext: UserDefaults.questionContext
           ).get { jsonDict in
            successAction?(jsonDict.success)
        }
    }

不幸的是,我不知道要提供什么代码来自动重现此问题。

我正在走出常规,发布了一个屏幕截图以证明错误正在显示。

enter image description here

2 个答案:

答案 0 :(得分:1)

看到带有隐式展开的Optional的错误消息的通常原因是,隐式展开不会通过分配来传播自身。例如,这是合法的:

    var s : String!
    print(s.count)

但这不是:

    let s2 = s
    print(s2.count) // value must be unwrapped

答案 1 :(得分:1)

我知道了,我更改了saveSelection的方法签名以要求另一个参数,但是我忘记在调用中添加默认值或添加参数。我不知道为什么编译器没有告诉我...