请向我解释错误的性质:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序
Posted
技术标签:
【中文标题】请向我解释错误的性质:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序【英文标题】:Kindly explain me about the nature of the error : Terminating app due to uncaught exception 'NSInvalidArgumentException' 【发布时间】:2020-05-04 18:15:29 【问题描述】:我正在 Xcode 版本 11.4.1 (11E503a) 中创建我的第一个 ios 应用程序。我正在使用 Swift 5.1 和 Storyboard 来构建用户界面。我在单个视图应用程序中有五个 TextField。用户应该在这些字段中输入数据,然后有一个发送按钮。当用户按下发送按钮时,字段中的数据应保存在字典中。我怀疑这是我做错了什么的部分。当我在模拟器中运行这个程序时,我不确定错误的性质。以下是代码,我写在ViewController.swift中
import UIKit
class ViewController: UIViewController
@IBOutlet weak var SerialNumber: UITextField!
@IBOutlet weak var WastebinType: UITextField!
@IBOutlet weak var EnterStatus: UITextField!
@IBOutlet weak var ActionRequired: UITextField!
@IBOutlet weak var EnterZone: UITextField!
@IBOutlet weak var SendButton: UIButton!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view.
@IBAction func SendTapped(_ sender: Any)
self.SaveFields()
func SaveFields() // This function is creating a dictionary of the data entered in the
let SN = self.SerialNumber.text! // text field
let WT = self.WastebinType.text!
let ER = self.EnterStatus.text!
let AR = self.ActionRequired.text!
let EZ = self.EnterZone.text!
let dict = ["SN": SN, "WT": WT, "ER" : ER, "AR" : AR, "EZ" : EZ]
print("/(dict)")
以下是我在控制台中收到的错误消息:
2020-05-04 23:30:24.164409+0530 Garbage management[8811:544439] -[Garbage_management.ViewController SendTap:]: unrecognized selector sent to instance 0x7fc9e550a2c0
2020-05-04 23:30:24.179445+0530 Garbage management[8811:544439] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Garbage_management.ViewController SendTap:]: unrecognized selector sent to instance 0x7fc9e550a2c0'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23e39f0e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff50ad79b2 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23e5ac34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 UIKitCore 0x00007fff48bf7058 -[UIResponder doesNotRecognizeSelector:] + 302
4 CoreFoundation 0x00007fff23e3e90c ___forwarding___ + 1436
5 CoreFoundation 0x00007fff23e40bf8 _CF_forwarding_prep_0 + 120
6 UIKitCore 0x00007fff48bc9235 -[UIApplication sendAction:to:from:forEvent:] + 83
7 UIKitCore 0x00007fff4857a9d3 -[UIControl sendAction:to:forEvent:] + 223
8 UIKitCore 0x00007fff4857ad1b -[UIControl _sendActionsForEvents:withEvent:] + 396
9 UIKitCore 0x00007fff48579c8c -[UIControl touchesEnded:withEvent:] + 497
10 UIKitCore 0x00007fff48c04a70 -[UIWindow _sendTouchesForEvent:] + 1359
11 UIKitCore 0x00007fff48c067f5 -[UIWindow sendEvent:] + 4501
12 UIKitCore 0x00007fff48be0c39 -[UIApplication sendEvent:] + 356
13 UIKitCore 0x00007fff48c6b096 __dispatchPreprocessedEventFromEventQueue + 7328
14 UIKitCore 0x00007fff48c6e262 __handleEventQueueInternal + 6565
15 UIKitCore 0x00007fff48c64dcb __handleHIDEventFetcherDrain + 88
16 CoreFoundation 0x00007fff23d9deb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17 CoreFoundation 0x00007fff23d9dddc __CFRunLoopDoSource0 + 76
18 CoreFoundation 0x00007fff23d9d5b4 __CFRunLoopDoSources0 + 180
19 CoreFoundation 0x00007fff23d981ae __CFRunLoopRun + 974
20 CoreFoundation 0x00007fff23d97ac4 CFRunLoopRunSpecific + 404
21 GraphicsServices 0x00007fff38b2fc1a GSEventRunModal + 139
22 UIKitCore 0x00007fff48bc7f80 UIApplicationMain + 1605
23 Garbage management 0x000000010081d4ab main + 75
24 libdyld.dylib 0x00007fff519521fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
请帮助我了解此错误的性质、为什么会出现以及如何缓解它。
【问题讨论】:
您需要将您的按钮连接到 IBAction 函数。好像没有连接了SendTap
与 SendTapped
.
问题已解决,解决方案由@Asperi 提供。就是你说的。谢谢大家帮助我。
【参考方案1】:
只需将您的操作重命名为
@IBAction func SendTap(_ sender: Any)
self.SaveFields()
很可能您是从情节提要建立连接,然后在编辑器中重命名它,当然,在情节提要中不会自动将其重命名。
【讨论】:
我按照你的指示,出现了同样的错误。但仍然感谢您的帮助。 我现在明白你的意思了,你想让我完全删除 IBAction 代码并断开连接,然后重新创建一个。知道了。谢谢。以上是关于请向我解释错误的性质:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序的主要内容,如果未能解决你的问题,请参考以下文章
由于未捕获的异常“NSInternalInconsistencyException”错误而终止应用程序
由于未捕获的异常“FirebaseShutdown”而终止应用程序,原因:“Firebase 错误
由于未捕获的异常“NSObjectInaccessibleException”而终止应用程序,原因:“CoreData 无法完成错误”