RoccoBerry RoccoBerry - 3 months ago 204x
iOS Question

NotificationCenter issue on Swift 3

I'm learning Swift 3 and I'm trying to using

. Here is my code:

func savePost(){
let postData = NSKeyedArchiver.archivedData(withRootObject: _loadedpost)
UserDefaults.standard().object(forKey: KEY_POST)
func loadPost(){
if let postData = UserDefaults.standard().object(forKey: KEY_POST) as? NSData{
if let postArray = NSKeyedUnarchiver.unarchiveObject(with: postData as Data) as? [Post]{
_loadedpost = postArray
NotificationCenter.default().post(NSNotification(name: "loadedPost" as NSNotification.Name, object: nil) as Notification)

and this is the observer:

override func viewDidLoad() {
NotificationCenter.default().addObserver(self, selector: Selector(("onPostLoaded")), name: "loadedPost", object: nil)

func numberOfSections(in tableView: UITableView) -> Int {
return 1

It always gives me the error "signal SIGBRT". When I try to change the name in the observer, it's not an error, but obviously it didn't show anything. How do I fix this?


Use Like:

NSNotificationCenter.defaultCenter().postNotificationName("YourNotificationName", object: nil)

and AddObserver Like:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourClassName.methodName), name: "YourNotificationName", object: nil)