0
我想建立一個使用swift 3和Xcode的小費計算器。我已經寫了一個函數,我試圖簡單地測試它,並確保它正在做它應該做的事情,並且稍後我會擔心格式化。但是,即使沒有任何構建或運行錯誤,當我嘗試在模擬器中運行它時,它也會崩潰。關於我可能收到的任何答案,我對此很新,而我的老師也沒有讓我們使用一本書,所以我不太熟悉正確的術語。我粘貼了我的代碼和崩潰日誌。我怎樣才能阻止我的迅速的應用程序崩潰在模擬器
//
// ViewController.swift
// myTipCalculator
//
// Created by Mac User on 2/11/17.
// Copyright © 2017 Mac User. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var billAmount: UITextField!
@IBOutlet weak var fifteenTip: UIButton!
@IBOutlet weak var twentyTip: UIButton!
@IBOutlet weak var twentyFiveTip: UIButton!
@IBOutlet weak var tipTotal: UILabel!
@IBOutlet weak var grandTotal: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func calcTip(num1: Double, num2: Double){
print("Tip: \(num1 * num2)")
}
@IBAction func calcFifteen(_ sender: Any) {
let billA = Double(billAmount.text!)
let tipA = 0.15
calcTip(num1: billA!, num2: tipA)
}
@IBAction func calcTwenty(_ sender: Any) {
let billA = Double(billAmount.text!)
let tipA = 0.20
calcTip(num1: billA!, num2: tipA)
}
/*@IBAction func calcTwentyFive(_ sender: Any) {
let billA = Double(billAmount.text!)
let tipA = 0.25
calcTip(num1: billA!, num2: tipA)
}*/
}
2017-02-11 18:56:21.596 myTipCalculator[7087:101844] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<myTipCalculator.ViewController 0x7f89d3807b80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key calcTwentyFive.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000108692d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010590b21e objc_exception_throw + 48
2 CoreFoundation 0x0000000108692c99 -[NSException raise] + 9
3 Foundation 0x00000001054199df -[NSObject(NSKeyValueCoding) setValue:forKey:] + 291
4 UIKit 0x0000000105f75293 -[UIViewController setValue:forKey:] + 88
5 UIKit 0x00000001061e979e -[UIRuntimeOutletConnection connect] + 109
6 CoreFoundation 0x00000001086379e0 -[NSArray makeObjectsPerformSelector:] + 256
7 UIKit 0x00000001061e8122 -[UINib instantiateWithOwner:options:] + 1867
8 UIKit 0x0000000105f7b9c5 -[UIViewController _loadViewFromNibNamed:bundle:] + 386
9 UIKit 0x0000000105f7c2e7 -[UIViewController loadView] + 177
10 UIKit 0x0000000105f7c61c -[UIViewController loadViewIfRequired] + 201
11 UIKit 0x0000000105f7ce70 -[UIViewController view] + 27
12 UIKit 0x0000000105e464b5 -[UIWindow addRootViewControllerViewIfPossible] + 71
13 UIKit 0x0000000105e46c06 -[UIWindow _setHidden:forced:] + 293
14 UIKit 0x0000000105e5a519 -[UIWindow makeKeyAndVisible] + 42
15 UIKit 0x0000000105dd2f8d -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4818
16 UIKit 0x0000000105dd90ed -[UIApplication _runWithMainScene:transitionContext:completion:] + 1731
17 UIKit 0x0000000105dd626d -[UIApplication workspaceDidEndTransaction:] + 188
18 FrontBoardServices 0x0000000109dce6cb __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
19 FrontBoardServices 0x0000000109dce544 -[FBSSerialQueue _performNext] + 189
20 FrontBoardServices 0x0000000109dce8cd -[FBSSerialQueue _performNextFromRunLoopSource] + 45
21 CoreFoundation 0x0000000108637761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
22 CoreFoundation 0x000000010861c98c __CFRunLoopDoSources0 + 556
23 CoreFoundation 0x000000010861be76 __CFRunLoopRun + 918
24 CoreFoundation 0x000000010861b884 CFRunLoopRunSpecific + 420
25 UIKit 0x0000000105dd4aea -[UIApplication _run] + 434
26 UIKit 0x0000000105ddac68 UIApplicationMain + 159
27 myTipCalculator 0x000000010532860f main + 111
28 libdyld.dylib 0x000000010963c68d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
這可能是因爲你有一個或多個不好的渠道。轉到故事板並右鍵單擊您的控制器,然後查看每個連接是否正常。如果不是,它會顯示黃色警告標誌。如果它是黃色的,只需將其刪除並再次進行連接即可。我希望這有幫助。 –