Swift:错误:在隐式展开可选值时意外发现 nil
Posted
技术标签:
【中文标题】Swift:错误:在隐式展开可选值时意外发现 nil【英文标题】:Swift: error: Unexpectedly found nil while implicitly unwrapping an Optional value 【发布时间】:2020-12-28 05:49:41 【问题描述】:我收到此错误代码:
错误:执行被中断,原因:EXC_BAD_INSTRUCTION (代码=EXC_I386_INVOP,子代码=0x0)。该过程已留在 中断点,使用“thread return -x”返回 表达式求值前的状态。
PlayGround 说:
致命错误:在隐式展开时意外发现 nil 可选值:文件 BilanDico-04.playground,第 137 行
这是我的代码:
import Foundation
import UIKit
let TableauDesBesoinsQuotidienEnVitaminesretenues : [String : Float] = [
"aRetinol" : 800,
"aBetacarotene" : 2100,
"vitamineC" : 80,
"vitamineB1" : 1.1,
"calcium" : 800,
"omega3" : 2
]
let TableauDesVitaminesRetenuesPourObjectifs : [String] = [
"aRetinol",
"aBetacarotene",
"vitamineC",
"vitamineB1",
"calcium",
"omega3"
]
class AlimentObject
var id = UUID().uuidString
var nomAliment = "Nouvel aliment"
var poids : Float = 100
var calories : Float = 0
var proteines : Float = 0
var lipides : Float = 0
var glucides : Float = 0
var aRetinol : Float = 0
var aBetacarotene : Float = 0
var vitamineC : Float = 0
var vitamineB1 : Float = 0
var calcium : Float = 0
var omega3 : Float = 0
var comments : String = ""
var premierBooleen: Bool
var optimize: Bool
init(nomAliment: String, poids: Float, calories: Float, proteines: Float, lipides: Float, glucides: Float, aRetinol: Float, aBetacarotene: Float, vitamineC: Float, vitamineB1: Float, calcium: Float, omega3: Float, comments:String, premierBooleen: Bool, optimize: Bool)
self.nomAliment = nomAliment
self.poids = poids
self.calories = calories
self.proteines = proteines
self.lipides = lipides
self.glucides = glucides
self.aRetinol = aRetinol
self.aBetacarotene = aBetacarotene
self.vitamineC = vitamineC
self.vitamineB1 = vitamineB1
self.calcium = calcium
self.omega3 = omega3
self.comments = comments
self.premierBooleen = false
self.optimize = false
convenience init()
self.init(nomAliment: "Aliment intialisé à zéro", poids: 100, calories: 0, proteines: 0, lipides: 0, glucides: 0, aRetinol: 0, aBetacarotene: 0, vitamineC: 0, vitamineB1: 0, calcium: 0, omega3: 0, comments: "", premierBooleen: false, optimize: false)
class Donnees
static var AlimentListWithSections = [[AlimentObject]]()
static let list1 = [
AlimentObject(nomAliment: "Blanc de poulet", poids: 100, calories: 108, proteines: 24, lipides: 1, glucides: 0, aRetinol: 8, aBetacarotene: 0, vitamineC: 0, vitamineB1: 0.08, calcium: 8, omega3: 0.03, comments: "sdsdsdsdsd sdsddsdsd", premierBooleen: false, optimize: false),
AlimentObject(nomAliment: "Avocat", poids: 100, calories: 147, proteines: 1.76, lipides: 13.80, glucides: 1.57, aRetinol: 0, aBetacarotene: 55, vitamineC: 5.49, vitamineB1: 0.05, calcium: 11.2, omega3: 0.08, comments: "aaaaaaaaa sdsddsdsd", premierBooleen: false, optimize: false),
AlimentObject(nomAliment: "Amandes", poids: 100, calories: 634, proteines: 21, lipides: 53, glucides: 8, aRetinol: 8, aBetacarotene: 1, vitamineC: 0.8, vitamineB1: 0.18, calcium: 248, omega3: 0.06, comments: "fffffff sdsddsdsd", premierBooleen: false, optimize: false)
]
static let list2 = [
AlimentObject(nomAliment: "Poivron Jaune", poids: 100, calories: 29.2, proteines: 1, lipides: 5.42, glucides: 0.21, aRetinol: 0, aBetacarotene: 120, vitamineC: 184, vitamineB1: 0.03, calcium: 11, omega3: 0.02, comments: "lllllllssss", premierBooleen: false, optimize: false),
AlimentObject(nomAliment: "Saumon cru", poids: 100, calories: 192, proteines: 20.5, lipides: 12.2, glucides: 0, aRetinol: 8, aBetacarotene: 0, vitamineC: 1.6, vitamineB1: 0.21, calcium: 8, omega3: 1.79, comments: "eeeeeeee", premierBooleen: false, optimize: false)
]
static let list3 : [AlimentObject] = [
AlimentObject(nomAliment: "Avocat", poids: 100, calories: 147, proteines: 1.76, lipides: 13.80, glucides: 1.57, aRetinol: 0, aBetacarotene: 55, vitamineC: 5.49, vitamineB1: 0.05, calcium: 11.2, omega3: 0.08, comments: "aaaaaaaaa sdsddsdsd", premierBooleen: false, optimize: false),
]
static let list4 : [AlimentObject] = [
AlimentObject(nomAliment: "Amandes", poids: 100, calories: 634, proteines: 21, lipides: 53, glucides: 8, aRetinol: 8, aBetacarotene: 1, vitamineC: 0.8, vitamineB1: 0.18, calcium: 248, omega3: 0.06, comments: "fffffff sdsddsdsd", premierBooleen: false, optimize: false)
]
static let list5 : [AlimentObject] = []
static let listVide : [AlimentObject] = []
static let listDuJour = [list1, list2, list3, list4, list5]
class AlimentFunctions
// sommer des Float d'une macro du Menu et sortir un Float
static func sumDUneMacroDuMenuToFloat(NameList list: [[AlimentObject]], macro: String) -> Float
var totalsection: Float = 0.0
var total: Float = 0.0
list.forEach
totalsection = 0
$0.forEach( (aliment) in
if let value = Mirror(reflecting: aliment).children.first(where: $0.label == macro )?.value as? Float
totalsection += value
)
total += totalsection
return total
class TotauxVitamineMineraux
static func DicoDesTotauxPaffonesEtNonPlaffonesDesVitamines(with menu: [[AlimentObject]], tableauDesNomsDesMacrosRetenues: [String], tableauDesValeursDesMacros: [String: Float]) -> [String: (Float, Float, Float, Float, Float)]
var dico: [String: (Float, Float, Float, Float, Float)]!
for macro in tableauDesNomsDesMacrosRetenues
let premier = tableauDesValeursDesMacros[macro]!
let second = AlimentFunctions.sumDUneMacroDuMenuToFloat(NameList: menu, macro: macro)
let troisieme = second / tableauDesValeursDesMacros[macro]! * 100
var quatrieme = second
if second > 100 quatrieme = 100
if second <= 100 quatrieme = second
let cinquieme = quatrieme / tableauDesValeursDesMacros[macro]! * 100
dico[macro] = (premier, second, troisieme, quatrieme, cinquieme)
return dico
let dico = TotauxVitamineMineraux.DicoDesTotauxPaffonesEtNonPlaffonesDesVitamines(with: Donnees.listDuJour, tableauDesNomsDesMacrosRetenues: TableauDesVitaminesRetenuesPourObjectifs, tableauDesValeursDesMacros: TableauDesBesoinsQuotidienEnVitaminesretenues)
print(dico)
错误在这一行:
dico[macro] = (premier, second, troisieme, quatrieme, cinquieme)
【问题讨论】:
代码中的第 137 行在哪里(发生错误的位置)? 在这一行:dico[macro] = (premier, second, troisieme, quatrieme, cinquieme) 【参考方案1】:var dico: [String: (Float, Float, Float, Float, Float)]!
保证该字典将存在,但没有创建它。然后,当您尝试将您说不会为 nil 的内容放入 nil 字典中时,程序会崩溃。
【讨论】:
那我该怎么办? 在尝试将内容放入字典之前创建字典。以上是关于Swift:错误:在隐式展开可选值时意外发现 nil的主要内容,如果未能解决你的问题,请参考以下文章
UITableViewCell 中的 AVPlayer 导致“在隐式展开可选值时意外发现 nil”错误
在隐式展开可选值时意外发现 nil (UICollectionView)
想要将 Slidervalue 设置为 UserDefault:线程 1:致命错误:在隐式展开可选值时意外发现 nil [重复]
不确定如何为现有代码正确设置我的变量。致命错误:在隐式展开可选值时意外发现 nil