【问题标题】:Getting a Swift run-time error (NSException)出现 Swift 运行时错误 (NSException)
【发布时间】:2016-02-13 19:14:33
【问题描述】:

我对 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!"
            }

        }
 }

请某人或某些人对这种情况有所了解。谢谢!

【问题讨论】:

  • 您是否将故事板中的标签和按钮连接到视图控制器?您可以右键单击标签和按钮以验证它们实际上是否正确连接

标签: swift swift2 xcode7


【解决方案1】:

您在情节提要或 .xib 文件中为视图控制器定义了出口。其中之一称为“btnRoll”,并在某个时候链接到控制器。代码中不存在匹配的属性。

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

【讨论】:

  • 您可以通过右键单击 btnRoll 对象来断开它并删除到您的视图控制器的出口
  • 谢谢,我刚刚创建了一个新项目来解决我遇到的问题。不知道这是怎么发生的,但谢谢。我会更加小心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
相关资源
最近更新 更多