可选类型的值必须解包以引用成员,但对于隐式解包的可选 属性
Value of optional type must be unwrapped to refer to member, BUT its for an implicitly unwrapped optional property
为什么我会收到这个?它是一个隐式展开的可选 属性。我不认为我应该得到这个错误。我尝试清理我的构建文件夹并删除派生数据,然后关闭 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)
}
}
不幸的是,我不知道要提供什么代码来自动重现这个问题。
我正在超越常规并发布屏幕截图以证明错误正在显示。
看到带有隐式解包 Optional 的错误消息的通常原因是隐式解包不会通过赋值传播自身。例如,这是合法的:
var s : String!
print(s.count)
但这不是:
let s2 = s
print(s2.count) // value must be unwrapped
我想通了,我更改了 saveSelection
的方法签名以需要另一个参数,但我忘记添加默认值或在调用中添加参数。我不知道为什么编译器不告诉我,而是...
为什么我会收到这个?它是一个隐式展开的可选 属性。我不认为我应该得到这个错误。我尝试清理我的构建文件夹并删除派生数据,然后关闭 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)
}
}
不幸的是,我不知道要提供什么代码来自动重现这个问题。
我正在超越常规并发布屏幕截图以证明错误正在显示。
看到带有隐式解包 Optional 的错误消息的通常原因是隐式解包不会通过赋值传播自身。例如,这是合法的:
var s : String!
print(s.count)
但这不是:
let s2 = s
print(s2.count) // value must be unwrapped
我想通了,我更改了 saveSelection
的方法签名以需要另一个参数,但我忘记添加默认值或在调用中添加参数。我不知道为什么编译器不告诉我,而是...