ScottyBlades
ScottyBlades

Reputation: 13993

Value of optional type must be unwrapped to refer to member, BUT its for an implicitly unwrapped optional property

Why am I getting this? Its an implicitly unwrapped optional property. I don't think I should be getting this error. I tried cleaning my build folder and deleting derived data, and closing Xcode. Nothing worked.

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
    }
}

Method definition

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)
        }
    }

Unfortunately, I don't know what code to provide to recreate this issue atomically.

I am stepping outside of the norm and posting a screen shot to prove that the error is showing.

enter image description here

Upvotes: 0

Views: 6026

Answers (2)

ScottyBlades
ScottyBlades

Reputation: 13993

I figured it out, I changed the method signature for saveSelection to require another parameter, but I forgot to add a default value or add the argument in the call. I don't know why the compiler wasn't telling me that instead...

Upvotes: 1

matt
matt

Reputation: 535178

The usual reason for seeing this error message with an implicitly unwrapped Optional is that implicit unwrapping does not promulgate itself thru assignment. For example, this is legal:

    var s : String!
    print(s.count)

But this is not:

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

Upvotes: 2

Related Questions