获取Swift运行时错误(NSException)

时间:2016-02-13 19:14:34

标签: swift swift2 xcode7

我对Swift编程比较陌生,我得到的运行时间让我感到非常兴奋,而且我不确定为什么会出现这种情况,因为源代码看起来是正确的。

运行时错误导致我在控制台中执行此操作:

'[<UIViewController 0x7ff27051a5f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key btnRoll.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000103744e65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000105484deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000103744aa9 -[NSException raise] + 9
    3   Foundation                          0x0000000103b0d9bb -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
    4   UIKit                               0x00000001040f0320 -[UIViewController setValue:forKey:] + 88
    5   UIKit                               0x000000010431ef41 -[UIRuntimeOutletConnection connect] + 109
    6   CoreFoundation                      0x00000001036854a0 -[NSArray makeObjectsPerformSelector:] + 224
    7   UIKit                               0x000000010431d924 -[UINib instantiateWithOwner:options:] + 1864
    8   UIKit                               0x00000001040f6eea -[UIViewController _loadViewFromNibNamed:bundle:] + 381
    9   UIKit                               0x00000001040f7816 -[UIViewController loadView] + 178
    10  UIKit                               0x00000001040f7b74 -[UIViewController loadViewIfRequired] + 138
    11  UIKit                               0x00000001040f82e7 -[UIViewController view] + 27
    12  UIKit                               0x0000000103fceab0 -[UIWindow addRootViewControllerViewIfPossible] + 61
    13  UIKit                               0x0000000103fcf199 -[UIWindow _setHidden:forced:] + 282
    14  UIKit                               0x0000000103fe0c2e -[UIWindow makeKeyAndVisible] + 42
    15  UIKit                               0x0000000103f59663 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131
    16  UIKit                               0x0000000103f5fcc6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1760
    17  UIKit                               0x0000000103f5ce7b -[UIApplication workspaceDidEndTransaction:] + 188
    18  FrontBoardServices                  0x0000000107319754 -[FBSSerialQueue _performNext] + 192
    19  FrontBoardServices                  0x0000000107319ac2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    20  CoreFoundation                      0x0000000103670a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    21  CoreFoundation                      0x000000010366695c __CFRunLoopDoSources0 + 556
    22  CoreFoundation                      0x0000000103665e13 __CFRunLoopRun + 867
    23  CoreFoundation                      0x0000000103665828 CFRunLoopRunSpecific + 488
    24  UIKit                               0x0000000103f5c7cd -[UIApplication _run] + 402
    25  UIKit                               0x0000000103f61610 UIApplicationMain + 171
    26  Craps                               0x00000001035652dd main + 109
    27  libdyld.dylib                       0x0000000105f8d92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我在我创建的迷你游戏中只有30行代码,我不明白为什么会出现错误:

 //
    //  ViewController.swift
    //  Craps
    //
    //  Created by Lamido Tijjo on 2/11/16.
    //  Copyright © 2016 YauwaSarki. All rights reserved.
    //

    import UIKit


    class CrapsViewController: UIViewController {

        var rollDice1: Int = 0
        var rollDice2: Int = 0
        var rollTotal: Int = 0

        @IBOutlet var lblRollOne: UILabel!
        @IBOutlet var lblRollTwo: UILabel!
        @IBOutlet var lblTotalRoll: UILabel!

        @IBAction func btnRollDice(sender: AnyObject) {
            rollDice1 = Int(arc4random() % 6) + 1
            rollDice2 = Int(arc4random() % 6) + 1

            rollTotal = rollDice1 + rollDice2
            lblRollOne.text = String(rollDice1)
            lblRollTwo.text = String(rollDice2)

            if rollTotal != 7 || rollTotal != 11 {
                lblTotalRoll.text = "Sorry you rolled a \(rollTotal), please try again!"
            } else {
                lblTotalRoll.text = "Congrats! you rolled a \(rollTotal), play again if you like!"
            }

        }
 }

请某人或某些人请了解情况。谢谢!

1 个答案:

答案 0 :(得分:2)

您在storyboard或.xib文件中为视图控制器定义了出口。其中一个被称为“btnRoll”,并且在某些时候与控制器相关联。代码中不存在匹配属性。

可能的原因是:您删除了代码而未取消链接插座,或者您将错误的视图控制器定义为视图的所有者。