2016-02-13 43 views
0

我对Swift编程比较陌生,而且我得到的一个运行时非常喜欢我,我不确定为什么会出现这种情况代码看起来正确。获取Swift运行时错误(NSException)

运行时错误导致我这个控制台:

'[<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!" 
      } 

     } 
} 

有人或某些人请关注一下情况。谢谢!

+0

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

回答

2

您在故事板或.xib文件中为视图控制器定义了出口。其中之一被称为“btnRoll”,并在某个时刻与控制器相连。代码中不存在匹配的属性。

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

+0

您可以通过右键单击btnRoll对象并将视频控制器的插座删除来断开它连接 – Eric

+0

谢谢我刚刚完成并创建了一个新项目,规避我遇到的问题。不知道如何发生,但谢谢。我会更加小心。 – Linuxn00b