So, can somebody at Apple please update the Notification Programming Topics documentation for Swift 2.2? In the meantime...
I've got a huge pile of iOS code converted from Objective-C to Swift, and so I am sitting on a lot of addObserver(..., selector: ...) calls. Like, dozens of calls. With the new warning to use #selector instead of a literal in Swift 2.2, it's got me thinking the converted code isn't sufficiently Swift-y (at least, I dont want to take the suggested fix, which makes the code super-duper unwieldy), so, I'm looking for other developers to chime in and either agree or disagree with the following.
I think the best pratice in Swift should be to not perpetuate Objective-C selectors, but instead use blocks. There's a call on NSNotificationCenter called addObserverForName(...) that takes at least one extra parameter, an NSOperationQueue, and a block.
So.
Yes? This is the right call for Swift-y code? For example, in converted code, I might have:
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "didChangePreferredContentSize:",
name: UIContentSizeCategoryDidChangeNotification,
object :nil)
And I might have a selector declared:
func didChangePreferredContentSize(notification: NSNotification) {
updatePreferredFontAdjustment()
}
And in Swift 2.2, rather than add #selector(ClassName.didChangePreferredContentSize(_:)) which is IMHO garbage, I think the preferred Swift syntax should be:
NSNotificationCenter.defaultCenter().addObserverForName(UIContentSizeCategoryDidChangeNotification, object: nil, queue: nil) { (notification) in
self.updatePreferredFontAdjustment()
}
Yes?