简体   繁体   中英

Swift Stopwatch App crashing, don't know where I'm going wrong

I am trying to make a stopwatch app for a coding class I am taking. I have no errors before I run the code, but once I run it it crashes after "build succeeds".

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    var timer = NSTimer()

    var count = 0

    func updateTime() {

        count++

        timeLabel.text = "\(count)"

    }

    @IBOutlet weak var timeLabel: UILabel!

    @IBAction func playButton(sender: AnyObject) {

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)

    }

    @IBAction func pauseButton(sender: AnyObject) {

    }

    @IBAction func resetButton(sender: AnyObject) {

    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Here is the output console:

2015-07-23 12:52:26.877 Stop Watch[7472:467329] *** Terminating app 
due to uncaught exception 'NSUnknownKeyException', reason:    
'[<Stop_Watch.ViewController   0x7ffdf1435b60> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key play.'

*** First throw call stack:
(
  0   CoreFoundation       0x000000010a814c65 __exceptionPreprocess + 165
  1   libobjc.A.dylib      0x000000010c37fbb7 objc_exception_throw + 45
  2   CoreFoundation       0x000000010a8148a9 -[NSException raise] + 9
  3   Foundation           0x000000010ac32b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
  4   CoreFoundation       0x000000010a75cd50 -[NSArray makeObjectsPerformSelector:] + 224
  5   UIKit                0x000000010b38b4eb -[UINib instantiateWithOwner:options:] + 1506
  6   UIKit                0x000000010b1e36d8 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
  7   UIKit                0x000000010b1e3cc8 -[UIViewController loadView] + 109
  8   UIKit                0x000000010b1e3f39 -[UIViewController loadViewIfRequired] + 75
  9   UIKit                0x000000010b1e43ce -[UIViewController view] + 27
  10  UIKit                0x000000010b0ff289 -[UIWindow addRootViewControllerViewIfPossible] + 58
  11  UIKit                0x000000010b0ff64f -[UIWindow _setHidden:forced:] + 247
  12  UIKit                0x000000010b10bde1 -[UIWindow makeKeyAndVisible] + 42
  13  UIKit                0x000000010b0af417 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
  14  UIKit                0x000000010b0b219e -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
  15  UIKit                0x000000010b0b1095 -[UIApplication workspaceDidEndTransaction:] + 179
  16  FrontBoardServices   0x000000010de7c5e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
  17  CoreFoundation       0x000000010a74841c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
  18  CoreFoundation       0x000000010a73e165 __CFRunLoopDoBlocks + 341
  19  CoreFoundation       0x000000010a73df25 __CFRunLoopRun + 2389
  20  CoreFoundation       0x000000010a73d366 CFRunLoopRunSpecific + 470
  21  UIKit                0x000000010b0b0b02 -[UIApplication _run] + 413
  22  UIKit                0x000000010b0b38c0 UIApplicationMain + 1282
  23  Stop Watch           0x000000010a618fa7 main + 135
  24  libdyld.dylib        0x000000010cab5145 start + 1
  25  ???                  0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

After looking at the console log, I am pretty sure the crash occurs because of a missing outlet connection.

Press Cmd+Shift+F and type 'play' in the search field. Look for a entry in your storyboard file, of the form ...: Outlet = "play" .

Click on that entry and remove it by clicking on the 'x' button next to play .

This should fix the crash.

Whenever you see an error of the form: ... this class is not key value coding-compliant for the key ... you should check if there is a missing outlet in your storyboard or xib.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM