2016-02-13 57 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