-2

How do I convert these statements to use the #selector statement. The statements work fine but gives warnings in Xcode, which I dislike.

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • 4
    just click on the warning and press enter – Dravidian Aug 22 '16 at 19:47
  • 2
    Or 10 seconds of search: http://stackoverflow.com/a/38841125/1187415. – Martin R Aug 22 '16 at 19:47
  • Call them `#selector(keyboardWillShow)` and `#selector(keyboardWillHide)`. – matt Aug 22 '16 at 19:48
  • @matt: How is this a duplicate of http://stackoverflow.com/questions/35658334/how-do-i-resolve-ambiguous-use-of-compile-error-with-swift-selector-syntax? – Martin R Aug 22 '16 at 19:50
  • @MartinR The answer tells everything the OP could possibly want to know about this syntax. – matt Aug 22 '16 at 20:06
  • @matt: But the *question* is different. "Duplicate" means: "This question has been asked before ..." – But never mind, this question isn't worth arguing about it. – Martin R Aug 22 '16 at 20:10
  • Possible duplicate of http://stackoverflow.com/questions/36147831/syntax-selector-swift-2-2 (Swift 2), http://stackoverflow.com/questions/37825327/swift-3-nsnotificationcenter-keyboardwillshow-hide (Swift 3). – Martin R Aug 22 '16 at 20:11
  • @MartinR That is a common misconception, but it's wrong. The question is _never_ going to be identically the same. "Duplicate" means the question has an answer that fully addresses the OP's question (as the SO notice says). – matt Aug 22 '16 at 20:40
  • @matt: That is not how I understand the (top voted) answer to http://meta.stackoverflow.com/questions/266244/can-a-question-be-a-duplicate-if-its-totally-different. "How do I convert Selector() to #selector" is not the same question as "How do I resolve ambiguous use of #selector?" – Martin R Aug 22 '16 at 20:58
  • I don't have many experience with developing for ios yet, so I didn't know this was the same in other #selector statements. Thank for helping it does work now. – Sander Wijnen Aug 23 '16 at 10:46

1 Answers1

3

In Swift 3, it looks like this:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
keithbhunter
  • 12,258
  • 4
  • 33
  • 58
  • thanks, so in my case this was the solution: NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil) – Sander Wijnen Aug 23 '16 at 10:44