xcode Swift:应用程序因未捕获的异常“NSUnknownKeyException”而终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26830142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: App terminating due to uncaught exception 'NSUnknownKeyException'
提问by
I'm developing a simple game and I am having trouble when a particular UIViewController (called SPSwipes.swift) is loaded. On the screen, there are three buttons: 5, 10, and 25. When one is picked, a variable is set in that sceneand the player moves onto the main Game: GameScene.swift.
我正在开发一个简单的游戏,但在加载特定的 UIViewController(称为SPSwipes.swift)时遇到了问题。On the screen, there are three buttons: 5, 10, and 25. When one is picked, a variable is set in that sceneand the player moves onto the main Game: GameScene.swift.
As far as I am aware, the variables are recognised in the GameScene, but there is a 'NSUnknownKeyException'error when I open up the SPSwipes view in the simulator. This is done by way of a segue. Here is the errorin more detail:
据我所知,变量在 GameScene 中被识别,但是当我在模拟器中打开SPSwipes 视图时出现“NSUnknownKeyException”错误。这是通过 segue 完成的。这是更详细的错误:
2014-11-09 15:32:43.718 Swipe Racer[2644:1620056] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Swipe_Racer.SPSwipes 0x7fbf52d461b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key fiveSwipes.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010c021f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010dd1bbb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010c021b79 -[NSException raise] + 9
3 Foundation 0x000000010c4397b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x000000010bf6be80 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x000000010cd28c7d -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x000000010cb87f98 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x000000010cb88588 -[UIViewController loadView] + 109
8 UIKit 0x000000010cb887f9 -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x000000010cb88c8e -[UIViewController view] + 27
10 UIKit 0x000000010caa7ca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010caa8041 -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010cab472c -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x000000010ca5f061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
14 UIKit 0x000000010ca61d2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
15 UIKit 0x000000010ca60bf2 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x000000011383b2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
17 CoreFoundation 0x000000010bf5753c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x000000010bf4d285 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x000000010bf4d045 __CFRunLoopRun + 2389
20 CoreFoundation 0x000000010bf4c486 CFRunLoopRunSpecific + 470
21 UIKit 0x000000010ca60669 -[UIApplication _run] + 413
22 UIKit 0x000000010ca63420 UIApplicationMain + 1282
23 Swipe Racer 0x000000010be380de top_level_code + 78
24 Swipe Racer 0x000000010be3811a main + 42
25 libdyld.dylib 0x000000010e507145 start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
As you can see, there is an issue with my first variable, fiveSwipes(and I would imagine that there would be errors with the two others). What does 'this class is not key value coding-compliant for the key fiveSwipes' mean? How should I go about fixing it?
正如你所看到的,是我的第一个变量的问题,fiveSwipes(我可以想象会有与其他两个错误)。't his class is not key value coding-compatible for the key FiveSwipes' 是什么意思?我应该如何修复它?
My SPSwipes.swiftfile:
我的SPSwipes.swift文件:
class SPSwipes: UIViewController {
var no_of_swipes = 0
@IBAction func fiveSwipes(sender: AnyObject) {
no_of_swipes = 5
//and so on
}
Thank you in advance,
先感谢您,
Will
将要
回答by
The problem was that the button was linked to numerous IBActions- three of which didn't exist. I right clickedon the button in the Interface Builderand removed the old and irrelevant links, fixing the problem.
问题是该按钮链接到许多 IBActions- 其中三个不存在。我右键单击Interface Builder 中的按钮并删除了旧的和不相关的链接,解决了问题。
回答by Suhaib
For me it was because I had multiple views connected to non existent IBOutlets
对我来说,这是因为我有多个视图连接到不存在的 IBOutlets
To get an idea how to find out:
要了解如何查找:
1- Click on the whole view controller
1- 单击整个视图控制器
2- go to : Show the connector Inspector
2-转到:显示连接器检查器
There you will find all the not connected outlets
在那里你会找到所有未连接的插座
回答by DPlusV
The exception is happening when attempting to load the view based on what you set up in Interface Builder. It's looking for fiveSwipes
and not finding it. This can happen if you edited your code and forgot to update a connection in IB.
尝试根据您在 Interface Builder 中设置的内容加载视图时发生异常。它正在寻找fiveSwipes
而不是找到它。如果您编辑了代码并忘记更新 IB 中的连接,就会发生这种情况。
Have you made a connection in IB to something called fiveSwipes
that isn't this function? Perhaps a variable called fiveSwipes
? The way to fix the problem would be to find the element with the bad connection in IB and clear that connection.
你是否在 IB 中建立了一个fiveSwipes
不是这个函数的东西?也许一个变量叫做fiveSwipes
? 解决问题的方法是在 IB 中找到连接不良的元素并清除该连接。
回答by SquareBox
This is an old question, the other answers din't help to fix my issue so would like to leave this answer here might help other people.
这是一个老问题,其他答案无助于解决我的问题,所以想把这个答案留在这里可能会帮助其他人。
Our app contains multiple "Targets" now it happens that the ViewController is pointing to specific target and only on that target this ViewController works. When running on other target the app crashes with this error.
我们的应用程序包含多个“目标”,现在恰好 ViewController 指向特定目标,并且仅在该目标上此 ViewController 才起作用。在其他目标上运行时,应用程序崩溃并出现此错误。
So, for those who has multiple "Targets" be sure to check the "Module" in the "Identity Inspector" and make sure that the "Inherit Module From Target" is checked.
因此,对于那些有多个“目标”的人,请务必检查“身份检查器”中的“模块”,并确保选中“从目标继承模块”。
回答by Michael Peterson
Another possible reason for this is if you are initializing your Controller using nibName:bundle: and you pass it the wrong nib name - some other nib that is not for this controller. The end result is the same, you'll likely have a bunch of @IBOutlets that do not match up between the Controller and the nib.
另一个可能的原因是,如果您使用 nibName:bundle: 初始化您的控制器,并且您将错误的笔尖名称传递给它 - 其他一些不适合此控制器的笔尖。最终结果是一样的,你可能会有一堆 @IBOutlets 在控制器和笔尖之间不匹配。