我正在iTunes U上进行斯坦福iOS课程,并且在调用getter方法时我会遇到奇怪的行为
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
var newNumber = true
var operandStack = Array<Double>()
var displayValue: Double{
get{
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set{
display.text = "\(newNumber)"
newNumber = true
}
}
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if(newNumber){
display.text = digit
newNumber = false
}else{
display.text = display.text! + digit
}
}
@IBAction func enter() {
if(!newNumber){
newNumber = true
}
operandStack.append(displayValue)
println("\(operandStack)")
}
}
当调用enter()
方法时,我遇到一个崩溃,断点指向displayValue
get方法中的return语句。如果我忽略了get方法并调用
operandStack.append(NSNumberFormatter().numberFromString(display.text!)!.doubleValue)
一切正常,但我不明白为什么get方法崩溃,是不是和上面的代码完全一样呢?
这是完整的回溯
* thread #1: tid = 0x133463, 0x000e4738 Kalkylator`Kalkylator.ViewController.displayValue.getter : Swift.Double(self=0x7986d560) + 24 at ViewController.swift:17, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x000e4738 Kalkylator`Kalkylator.ViewController.displayValue.getter : Swift.Double(self=0x7986d560) + 24 at ViewController.swift:17
frame #1: 0x000e57c2 Kalkylator`Kalkylator.ViewController.enter (self=0x7986d560)() -> () + 130 at ViewController.swift:39
frame #2: 0x000e5992 Kalkylator`@objc Kalkylator.ViewController.enter (Kalkylator.ViewController)() -> () + 34 at ViewController.swift:0
frame #3: 0x01d0c7cd libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84
frame #4: 0x00b1aa90 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 99
frame #5: 0x00b1aa22 UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
frame #6: 0x00c5b18a UIKit`-[UIControl sendAction:to:forEvent:] + 69
frame #7: 0x00c5b5a7 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 598
frame #8: 0x00c5a811 UIKit`-[UIControl touchesEnded:withEvent:] + 660
frame #9: 0x00b72cfa UIKit`-[UIWindow _sendTouchesForEvent:] + 874
frame #10: 0x00b737d6 UIKit`-[UIWindow sendEvent:] + 792
frame #11: 0x00b316d1 UIKit`-[UIApplication sendEvent:] + 242
frame #12: 0x00b41b08 UIKit`_UIApplicationHandleEventFromQueueEvent + 21484
frame #13: 0x00b15337 UIKit`_UIApplicationHandleEventQueue + 2300
frame #14: 0x0021706f CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
frame #15: 0x0020cb7d CoreFoundation`__CFRunLoopDoSources0 + 253
frame #16: 0x0020c0d8 CoreFoundation`__CFRunLoopRun + 952
frame #17: 0x0020ba5b CoreFoundation`CFRunLoopRunSpecific + 443
frame #18: 0x0020b88b CoreFoundation`CFRunLoopRunInMode + 123
frame #19: 0x03fa32c9 GraphicsServices`GSEventRunModal + 192
frame #20: 0x03fa3106 GraphicsServices`GSEventRun + 104
frame #21: 0x00b19106 UIKit`UIApplicationMain + 1526
frame #22: 0x000eec64 Kalkylator`main + 180 at AppDelegate.swift:12
frame #23: 0x02407ac9 libdyld.dylib`start + 1
答案 0 :(得分:0)
var newNumber = true
这是一个布尔值。
display.text = "\(newNumber)"
您正在将标签的文本设置为该布尔值
NSNumberFormatter().numberFromString(display.text
你正在从布尔值创建一个数字 - 而不是NSNumber - 这里。
你可能的意思是“newValue”。
答案 1 :(得分:0)
这是修改后的工作代码:
您遇到崩溃的行已被替换。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
var newNumber = true
var operandStack = Array<Double>()
var displayValue: Double {
get{
return (display.text! as NSString).doubleValue // Changed Line
}
set{
display.text = "\(newNumber)"
newNumber = true
}
}
@IBAction func appendDigit(sender: UIButton) {
let digit = sender.currentTitle!
if(newNumber){
display.text = digit
newNumber = false
}else{
display.text = display.text! + digit
}
}
@IBAction func enter() {
if(!newNumber){
newNumber = true
}
operandStack.append(displayValue)
println("\(operandStack)")
}
}
答案 2 :(得分:-1)
原来我只是意外地放置了一个断点而不知道它是什么。删除它当然解决了问题