Swift 4 - Notification Center addObserver issue

后端 未结 3 1335
梦谈多话
梦谈多话 2020-12-30 05:10

I\'m crashing and getting an unrecognized selector error every time a Notification arrives and the App tries to execute its associated method. Here

3条回答
  •  抹茶落季
    2020-12-30 05:29

    You can improve your code with these steps:

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

    And use it like this:

    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self,
                                   selector: #selector(YourClass.sayHello),
                                   name: .dataDownloadCompleted,
                                   object: nil)
    

    But as was already pointed out, issue is solved by changing to #selector

提交回复
热议问题