gpt4 book ai didi

swift - 获取 Swift 运行时错误 (NSException)

转载 作者:行者123 更新时间:2023-11-28 12:54:32 25 4
gpt4 key购买 nike

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

}
}

请某人或某些人解释一下情况。谢谢!

最佳答案

您在 Storyboard或 .xib 文件中为您的 View Controller 定义了 socket 。其中之一称为“btnRoll”,并在某个时候链接到 Controller 。代码中不存在匹配的属性。

可能的原因是:您删除了代码而没有断开 outlet 的链接,或者您将错误的 View Controller 定义为 View 的所有者。

关于swift - 获取 Swift 运行时错误 (NSException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com