快速通知中心使用未声明的类型名称错误

时间:2018-12-04 20:02:41

标签: ios swift

我遇到了最令人沮丧的错误。 我在应用程序中设置了Notifications,并且使用的是Swift 4.2。我正在使用新的语法。我设置的popupController如下所示

    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'

一些搜索后,我发现#selectors设置不正确时,这是一个古老的ObjC错误。 但是由于我使用的是新语法,所以这应该不是问题。 最后一个提示:当我将鼠标悬停在.saveFoodGroup)调用中的NotificatioCenter.default.post(name: .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
)

1 个答案:

答案 0 :(得分:0)

您发布通知的地方(可能是按钮操作)

  

-[unFatMobile.ScalePopUpController onBtPRew:]

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

Note :在复制IB内部的元素时也会发生这种情况,附加动作随它们一起复制,因此请确保断开无关的动作