如何阻止我的swift应用程序在模拟器中崩溃

时间:2017-02-12 00:57:45

标签: iphone swift xcode

我正在尝试使用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) 

1 个答案:

答案 0 :(得分:0)

尝试从此操作中删除评论:calcTwentyFive,然后运行您的应用。如果这对您没有帮助,请查看您的所有渠道和行动。

您似乎忘记在UIViewController中列出您的某个操作/插座。基本上,单击Main.StoryBoard中的每个项目,然后单击右侧的箭头以检查连接,如下图所示:

enter image description here

确保在UIViewController中找到所有插座和操作。

相关问题