我怎样才能打开剩余的秒数以防止它为零?
How can I unwrap seconds remaining to prevent it from being nil?
为什么我在展开可选值时收到“意外发现 nil?我检查了 timerSeconds 的值,它已正确分配给我想要分配给它的对象。但是,当我调用函数 StartTimer 我的应用程序时正在崩溃。
300 EggTimer/ViewController.swift:30: Fatal error: Unexpectedly found
nil while unwrapping an Optional value 2021-06-02 19:17:04.380375+1000
EggTimer[27674:932041] EggTimer/ViewController.swift:30: Fatal error:
Unexpectedly found nil while unwrapping an Optional value (lldb)
import UIKit
class ViewController: UIViewController {
let eggTimes : [String : Int] = ["Soft": 300, "Medium": 420, "Hard": 720]
var secondsRemaining: Int?
@IBAction func hardnessSelected(_ sender: UIButton) {
let hardness = sender.currentTitle!
let timerSeconds = eggTimes[hardness]!
print(timerSeconds)
//until here the code seems to work fine
startTimer(secondsRemaining: timerSeconds)
//call the function start timer and give the secondRemaining argument the value of timerSeconds
}
func startTimer (secondsRemaining: Int?){
//create a function called startTimer which accepts an interger as argument called secondsremaining
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
if self.secondsRemaining! > 0 {
//if the secondsRemaining >
print ("\(self.secondsRemaining ?? 0) seconds")
self.secondsRemaining! -= 1
}else {
Timer.invalidate()
}
}
}
}
注意在startTimer
中,self.secondsRemaining
与参数secondsRemaining
指的不是同一个东西:
var secondsRemaining: Int? // self.secondsRemaining
@IBAction func hardnessSelected(_ sender: UIButton) {
...
}
func startTimer (secondsRemaining: Int?){ // you never use this parameter
//create a function called startTimer which accepts an interger as argument called secondsremaining
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
// here you are referring to the var declared outside of the methods
// which you never assign anything to.
// this does not refer to the parameter
if self.secondsRemaining! > 0 {
一个简单的修复方法是将 self.secondsRemaining
设置为 startTimer
开头的参数 secondsRemaining
:
func startTimer (secondsRemaining: Int?){ // you never use this parameter
self.secondsRemaining = secondsRemaining
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
// same as before...
您好@dumanji,您还使用 ! 强制解包了两个常量。如果您尝试使用零值,这可能会崩溃。特别是如果您计划将此应用程序推向生产环境,这可能会导致意外的运行时错误和应用程序崩溃。
示例:
- 设硬度=sender.currentTitle!
- 让 timerSeconds = eggTimes[硬度]!
考虑使用 ?? (nil 合并运算符)在常量右侧提供默认值,以防可选的 returns nil.
可能的方法:
- 让 timerSeconds = eggTimes[硬度] ?? 420
如果有帮助请告诉我。
为什么我在展开可选值时收到“意外发现 nil?我检查了 timerSeconds 的值,它已正确分配给我想要分配给它的对象。但是,当我调用函数 StartTimer 我的应用程序时正在崩溃。
300 EggTimer/ViewController.swift:30: Fatal error: Unexpectedly found nil while unwrapping an Optional value 2021-06-02 19:17:04.380375+1000 EggTimer[27674:932041] EggTimer/ViewController.swift:30: Fatal error: Unexpectedly found nil while unwrapping an Optional value (lldb)
import UIKit
class ViewController: UIViewController {
let eggTimes : [String : Int] = ["Soft": 300, "Medium": 420, "Hard": 720]
var secondsRemaining: Int?
@IBAction func hardnessSelected(_ sender: UIButton) {
let hardness = sender.currentTitle!
let timerSeconds = eggTimes[hardness]!
print(timerSeconds)
//until here the code seems to work fine
startTimer(secondsRemaining: timerSeconds)
//call the function start timer and give the secondRemaining argument the value of timerSeconds
}
func startTimer (secondsRemaining: Int?){
//create a function called startTimer which accepts an interger as argument called secondsremaining
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
if self.secondsRemaining! > 0 {
//if the secondsRemaining >
print ("\(self.secondsRemaining ?? 0) seconds")
self.secondsRemaining! -= 1
}else {
Timer.invalidate()
}
}
}
}
注意在startTimer
中,self.secondsRemaining
与参数secondsRemaining
指的不是同一个东西:
var secondsRemaining: Int? // self.secondsRemaining
@IBAction func hardnessSelected(_ sender: UIButton) {
...
}
func startTimer (secondsRemaining: Int?){ // you never use this parameter
//create a function called startTimer which accepts an interger as argument called secondsremaining
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
// here you are referring to the var declared outside of the methods
// which you never assign anything to.
// this does not refer to the parameter
if self.secondsRemaining! > 0 {
一个简单的修复方法是将 self.secondsRemaining
设置为 startTimer
开头的参数 secondsRemaining
:
func startTimer (secondsRemaining: Int?){ // you never use this parameter
self.secondsRemaining = secondsRemaining
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (Timer) in
// same as before...
您好@dumanji,您还使用 ! 强制解包了两个常量。如果您尝试使用零值,这可能会崩溃。特别是如果您计划将此应用程序推向生产环境,这可能会导致意外的运行时错误和应用程序崩溃。
示例:
- 设硬度=sender.currentTitle!
- 让 timerSeconds = eggTimes[硬度]!
考虑使用 ?? (nil 合并运算符)在常量右侧提供默认值,以防可选的 returns nil.
可能的方法:
- 让 timerSeconds = eggTimes[硬度] ?? 420
如果有帮助请告诉我。