swift通知中心使用未声明类型名称错误



我遇到了最令人沮丧的错误。我正在我的应用程序中设置通知,并使用swift 4.2。我正在使用新语法。我设置的弹出式控制器如下

NotificationCenter.default.post(name: .saveFoodGroup, object: self)
dismiss(animated: true, completion: nil)

我创建了一个名为的NotificationCenterExtension.swift文件

extension Notification.Name {
static let saveFoodGroup = Notification.Name(rawValue: "saveFoodGroup")
}

在我的控制器中,我将观察者设置在.viewDidLoad()内,如下所示:

NotificationCenter.default.addObserver(
forName: .saveFoodGroup,
object: nil,
queue: OperationQueue.main ) {
(notification) in
let groupPopUp = notification.object as! ScalePopUpController
print(groupPopUp)
self.foodGroup = groupPopUp.textInput as? String
}

但在运行时,我不断收到这个错误:

unrecognised selector sent to instance 0x7fb9c7e23ef0'

经过一些搜索,我发现当#选择器设置不正确时,这是一个旧的ObjC错误。但由于我使用的是新语法,所以这应该不是问题。最后一条线索:当我将鼠标悬停在NotificatioCenter.default.post(name: .saveFoodGroup(调用中的.saveFoodGroup参数上时;我得到了这个神秘的暗示;expression produced error: error:

/var/folders/xj/f18tylg51kl33z6c6p2vh34r0000gn/T/expr15-a14342.swift:1:65:错误:使用未声明的类型"名称"`

有人能告诉我发生了什么吗?

完整错误消息:

2018-12-04 20:17:28.780056+0100 unFatMobile[47399:4108185] -[unFatMobile.ScalePopUpController onBtPRew:]: unrecognized selector sent to instance 0x7fb9c7e23ef0
2018-12-04 20:17:28.787522+0100 unFatMobile[47399:4108185] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[unFatMobile.ScalePopUpController onBtPRew:]: unrecognized selector sent to instance 0x7fb9c7e23ef0'
(
0   CoreFoundation                      0x000000010c6b91bb __exceptionPreprocess + 331
1   libobjc.A.dylib                     0x000000010a6d3735 objc_exception_throw + 48
2   CoreFoundation                      0x000000010c6d7f44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   UIKitCore                           0x000000010f091b4a -[UIResponder doesNotRecognizeSelector:] + 287
4   CoreFoundation                      0x000000010c6bded6 ___forwarding___ + 1446
5   CoreFoundation                      0x000000010c6bfda8 _CF_forwarding_prep_0 + 120
6   UIKitCore                           0x000000010f064ecb -[UIApplication sendAction:to:from:forEvent:] + 83
7   UIKitCore                           0x000000010eaa00bd -[UIControl sendAction:to:forEvent:] + 67
8   UIKitCore                           0x000000010eaa03da -[UIControl _sendActionsForEvents:withEvent:] + 450
9   UIKitCore                           0x000000010ea9f31e -[UIControl touchesEnded:withEvent:] + 583
10  UIKitCore                           0x000000010f0a00a4 -[UIWindow _sendTouchesForEvent:] + 2729
11  UIKitCore                           0x000000010f0a17a0 -[UIWindow sendEvent:] + 4080
12  UIKitCore                           0x000000010f07f394 -[UIApplication sendEvent:] + 352
13  UIKit                               0x0000000127f0b183 -[UIApplicationAccessibility sendEvent:] + 85
14  UIKitCore                           0x000000010f1545a9 __dispatchPreprocessedEventFromEventQueue + 3054
15  UIKitCore                           0x000000010f1571cb __handleEventQueueInternal + 5948
16  CoreFoundation                      0x000000010c61e721 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17  CoreFoundation                      0x000000010c61df93 __CFRunLoopDoSources0 + 243
18  CoreFoundation                      0x000000010c61863f __CFRunLoopRun + 1263
19  CoreFoundation                      0x000000010c617e11 CFRunLoopRunSpecific + 625
20  GraphicsServices                    0x00000001147d41dd GSEventRunModal + 62
21  UIKitCore                           0x000000010f06381d UIApplicationMain + 140
22  unFatMobile                         0x0000000109d56f47 main + 71
23  libdyld.dylib                       0x000000010db52575 start + 1
)

从发布通知的位置(可能是按钮操作(

-[unFatMobile.ScalePopUpController onBtPRew:]

有一个名为onBtPRew的操作方法,您将其链接到Ib,然后将其删除,这里的通知实现(selector&&@objc(与此崩溃无关,新的内联回调正在中工作

Note :当您复制IB内部的元素时,也会发生这种情况,所附的操作与它们一起复制,因此请确保断开不相关的操作

相关内容

最新更新