2

I'm new in Swift, i am using video lessons. But in current lesson i have an issue, and i can't solve it by myself.

The issue is with new #selector syntax in NSNotificationCenter, i used the old syntax and it didn't work.

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "onPostsLoaded:", name: "postsLoaded", object: nil)

        tableView.reloadData()
    }

    func onPostsLoaded(notif:AnyObject) {
        tableView.reloadData()
    }

}

Please check the Screenshot:How should I rewrite the yellow code (with selector) to make it work?

Thanks in advance

sschale
  • 5,168
  • 3
  • 29
  • 36
Rurom
  • 253
  • 3
  • 18
  • 1
    It's also works well : NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.onPostsLoaded(_:)), name: "postsLoaded", object: nil) – Mannopson Sep 17 '16 at 00:57

3 Answers3

4

Try this :

 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourViewController.onPostsLoaded(_:)), name: "postsLoaded", object: nil)

Don't forget to replace YourViewController by the name of your controller, hope it helps you.

Chajmz
  • 729
  • 5
  • 11
3

Try replacing with

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.onPostsLoaded(_:)), name: "postsLoaded", object: nil)
1

I guess the answers above are the best, but don't forget the "_" in the targetAction as well, since you use Swift 3.0, see my answer here:

https://stackoverflow.com/a/39537901/3143890

Community
  • 1
  • 1
LukeSideWalker
  • 7,399
  • 2
  • 37
  • 45