使用未解析的标识符 'timeR' -> 调用嵌套函数
Use of unresolved identifier 'timeR' -> Calling a nested function
import UIKit
class ViewController: UIViewController {
var timer:Timer?
@objc func onTimerFires() {
var timeLeft:Int = timeR()
timeLeft -= 1
print("\(timeLeft) seconds left")
if timeLeft <= 0 {
timer!.invalidate()
timer = nil
}
}
@IBAction func hardnessSelected(_ sender: UIButton) {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
let eggTimes:[String:Int] = [
"Soft":5,
"Medium":7,
"Hard":12,
]
let hardness = sender.currentTitle!
func timeR() -> Int {
if eggTimes[hardness]! == 5{
return(120)
}
else if eggTimes[hardness]! == 7{
return(420)
}
else {
return(720)
}
}
print(eggTimes[hardness]!)
}
}
我无法提取嵌套函数,有没有办法进行内部函数调用。谢谢
timeR
函数被定义为 hardnessSelected
内的局部函数,因此它仅在其包含函数 hardnessSelected
内可见。
如果你想把hardness
的值传递给onTimerFires
,你需要把它保存到ViewController
的实例属性中,这两个onTimerFires
和 hardnessSelected
可以访问。然后将timeR
移动到onTimerFires
,并传入hardness
的值。
class ViewController: UIViewController {
private let eggTimes:[String:Int] = [
"Soft":5,
"Medium":7,
"Hard":12,
]
var hardness: String?
var timer:Timer?
@objc func onTimerFires() {
func timeR(hardness: String) -> Int {
if eggTimes[hardness] == 5 {
return 120
} else if eggTimes[hardness] == 7{
return 420
} else {
return 720
}
}
var timeLeft:Int = timeR(hardness: hardness ?? "")
timeLeft -= 1
print("\(timeLeft) seconds left")
if timeLeft <= 0 {
timer!.invalidate()
timer = nil
}
}
@IBAction func hardnessSelected(_ sender: UIButton) {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
hardness = sender.currentTitle
print(time)
}
}
import UIKit
class ViewController: UIViewController {
var timer:Timer?
@objc func onTimerFires() {
var timeLeft:Int = timeR()
timeLeft -= 1
print("\(timeLeft) seconds left")
if timeLeft <= 0 {
timer!.invalidate()
timer = nil
}
}
@IBAction func hardnessSelected(_ sender: UIButton) {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
let eggTimes:[String:Int] = [
"Soft":5,
"Medium":7,
"Hard":12,
]
let hardness = sender.currentTitle!
func timeR() -> Int {
if eggTimes[hardness]! == 5{
return(120)
}
else if eggTimes[hardness]! == 7{
return(420)
}
else {
return(720)
}
}
print(eggTimes[hardness]!)
}
}
我无法提取嵌套函数,有没有办法进行内部函数调用。谢谢
timeR
函数被定义为 hardnessSelected
内的局部函数,因此它仅在其包含函数 hardnessSelected
内可见。
如果你想把hardness
的值传递给onTimerFires
,你需要把它保存到ViewController
的实例属性中,这两个onTimerFires
和 hardnessSelected
可以访问。然后将timeR
移动到onTimerFires
,并传入hardness
的值。
class ViewController: UIViewController {
private let eggTimes:[String:Int] = [
"Soft":5,
"Medium":7,
"Hard":12,
]
var hardness: String?
var timer:Timer?
@objc func onTimerFires() {
func timeR(hardness: String) -> Int {
if eggTimes[hardness] == 5 {
return 120
} else if eggTimes[hardness] == 7{
return 420
} else {
return 720
}
}
var timeLeft:Int = timeR(hardness: hardness ?? "")
timeLeft -= 1
print("\(timeLeft) seconds left")
if timeLeft <= 0 {
timer!.invalidate()
timer = nil
}
}
@IBAction func hardnessSelected(_ sender: UIButton) {
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
hardness = sender.currentTitle
print(time)
}
}