我遇到一个错误,提示在 Xcode 中发现未捕获的异常(SIGABRT 信号)

I am facing an error which says uncaught exception found (signal SIGABRT) in Xcode

我只是想在单击按钮时打印一些东西。

import UIKit

class ViewController: UIViewController {

    @IBAction func bu(_ sender: UIButton) {
        print("click")
    }

}

它在控制台中显示错误为

2017-05-22 18:28:58.751 will it work[24356:1074948] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button.' * First throw call stack: ( 0 CoreFoundation 0x0000000110a06b0b exceptionPreprocess + 171 1 libobjc.A.dylib
0x000000010ddcb141 objc_exception_throw + 48 2 CoreFoundation
0x0000000110a06a59 -[NSException raise] + 9 3 Foundation
0x000000010d8e0e8b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 292 4 UIKit 0x000000010e431644 -[UIViewController setValue:forKey:] + 87 5 UIKit 0x000000010e69e6b9 -[UIRuntimeOutletConnection connect] + 109 6
CoreFoundation 0x00000001109ace8d -[NSArray makeObjectsPerformSelector:] + 269 7 UIKit
0x000000010e69d06f -[UINib instantiateWithOwner:options:] + 1856 8
UIKit 0x000000010e437c73 -[UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x000000010e438589 -[UIViewController loadView] + 177 10 UIKit
0x000000010e4388ba -[UIViewController loadViewIfRequired] + 195 11 UIKit 0x000000010e43910a -[UIViewController view] + 27 12 UIKit 0x000000010e30163a -[UIWindow addRootViewControllerViewIfPossible] + 65 13 UIKit 0x000000010e301d20 -[UIWindow _setHidden:forced:] + 294 14 UIKit 0x000000010e314b6e -[UIWindow makeKeyAndVisible] + 42 15 UIKit
0x000000010e28e31f -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4346 16 UIKit 0x000000010e294584 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1709 17 UIKit 0x000000010e291793 -[UIApplication workspaceDidEndTransaction:] + 182 18 FrontBoardServices 0x00000001121065f6 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK
+ 24 19 FrontBoardServices 0x000000011210646d -[FBSSerialQueue _performNext] + 186 20 FrontBoardServices 0x00000001121067f6 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 21 CoreFoundation 0x00000001109acc01 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 22 CoreFoundation 0x00000001109920cf __CFRunLoopDoSources0 + 527 23 CoreFoundation 0x00000001109915ff __CFRunLoopRun + 911 24 CoreFoundation
0x0000000110991016 CFRunLoopRunSpecific + 406 25 UIKit
0x000000010e29002f -[UIApplication _run] + 468 26 UIKit
0x000000010e2960d4 UIApplicationMain + 159 27 will it work
0x000000010d638c17 main + 55 28 libdyld.dylib
0x000000011199665d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

我认为按钮甚至没有连接这里是图像:

请断开您的按钮插座与故事板的连接。

如果您可以 运行 在没有连接视图的情况下并且没有得到异常,那就有问题了。当加载 UIViewController 并且您尝试显示它时,它会使用视图来了解要显示的内容。仔细检查您的 class 和连接。如果您没有找到问题,post 更多信息,我们会尽力解决。

@Narasimha Reddy 请断开您的按钮插座与故事板的连接。

最重要的信息是

'[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button.'

查看您的代码,只有一个操作 bu 而不是 button,这很可能是错字。

所以替换

@IBAction func bu(_ sender: UIButton)

@IBAction func button(_ sender: UIButton)