Error with notification names while converting code to Swift 4.2 - nsnotifications

The code below was working fine before Swift 4.2:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
When I click the 'Fix' option, it becomes:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIResponder.keyboardWillShowNotification, object: nil)
But it is still marked an error. Here is the explanation:
Type 'NSNotification.Name' has no member 'UIResponder'
And then I tried to delete 'UIResponder':
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.
...but I don't know how should I complete it.

The correct form is:
UIResponder.keyboardWillShowNotification
...so, your code becomes:
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillChange(notification:)),
name: UIResponder.keyboardWillShowNotification,
object: nil
)
This is a known issue with Xcode 10. Automatic Fix-it is not working correctly for Swift 4.2 when it comes to correcting notification names.
In Swift 4.2, lots of Notification.Name instances became instance variables in other classes. For example, keyboardWillShowNotification is now an instance variable of UIResponder.

For someone else out there, I was building (what I thought was) a UI-Independent class and did not import UIKit.
Nothing worked until I added at the top of my file, this:
import UIKit
It appears some notifications (those in UIApplication, UIResponder etc..) may have been refactored into UIKIt.

The selected answer is incomplete and produce compilers error,
Cannot invoke 'addObserver' with an argument list of type
'(RegistrationViewController, selector: Selector, name:
NSNotification.Name)'
Here is the working format,
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)

Related

AVAudiosessionInterruptionNotification in Swift 3

This is the code I had in swift 2.
How do I use the same thing in swift 3?
NotificationCenter.default.addObserver(self, selector: "handleInterruption", name: AVAudiosessionInterruptionNotification, object: nil)
Thanks in advance!
Have a look at this fine answer
As it says:
All of the system notification types are now defined as static constants on Notification.Name; i.e. .UIApplicationDidFinishLaunching, .UITextFieldTextDidChange, etc.
So, in your case, you are probably looking for Notification.Name.AVAudioSessionInterruption
And I think this should work for you:
NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: .AVAudioSessionInterruption, object: nil)
Hope that helps.

AVAudion Session Interruption

` setSession()
UIApplication.shared.beginReceivingRemoteControlEvents()
becomeFirstResponder()
NotificationCenter.defaultCenter.addObserver(self, Selector("handleInterruption"), name: AVAudioSessionInterruptionNotification, object: nil)
//the Error is the line above this comment
player = Player()
player.playStream(fileUrl: "http:wwwwww.mp3")
updatePlayButton()`
Im trying to check when a phone call has finished by using AVAudiointerruptionNotification but I'm doing something wrong I keep getting the Argument labels _, _, name, object do not match any available overloads...
The new way of using the Notification center in swift 3 is as fallows. I was able to compile with no errors or warnings!
NotificationCenter.default.addObserver(self, selector:
(Selector("handleInterruption")), name:
NSNotification.Name.AVAudioSessionInterruption, object: nil)

swift AVPlayerItemDidPlayToEndTimeNotification not work

I create a class named DecryptAudioPlayer,it inherit NSObject,this class refrenced a AVPlayer,and observe the notice AVPlayerItemDidPlayToEndTimeNotification when init.like below:
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.playToEnd(_:)), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
and i have the method:
func playToEnd(notification:NSNotification) {
Log.printLog("notification:\(notification)")
}
but sometimes the player's ower can't recived the AVPlayerItemDidPlayToEndTimeNotification,i am very confused.it seems the AVPlayerItem may don't post AVPlayerItemDidPlayToEndTimeNotification when its end time ,who can tell me why?
I can get the stalled notification to solve the problem. So the question could be put down for a while.

Can't get iCloud metadataQuery to work

I have an issue with the iCloud metadataQuery.
I am trying to fetch some documents from iCloud but when I call the NSPredicate function I get the following error in the console of Xcode:
warning: could not load any Objective-C class information from the
dyld shared cache. This will significantly reduce the quality of type
information available.
Note: I am using Swift version 2.
This is my function:
var _iCloudQuery:NSMetadataQuery!
func iCloudQuery()->NSMetadataQuery?{
if (_iCloudQuery == nil) {
_iCloudQuery = NSMetadataQuery.init()
_iCloudQuery.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]
_iCloudQuery.predicate = NSPredicate(format: "(%K = '*')", NSMetadataItemURLKey)//
// Set observer here
NSNotificationCenter.defaultCenter().addObserver(self, selector: "processCloudQueryResults:", name: NSMetadataQueryDidFinishGatheringNotification, object: _iCloudQuery)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "processCloudQueryResults:", name: NSMetadataQueryDidUpdateNotification, object: _iCloudQuery)
}
return _iCloudQuery
}
I already figured out that processCloudQueryResults never gets fired because there are no items in icloudQuery but still there are documents in my iCloud container so there should.
This is a related topic with no solutions:
Searching for files using NSMetadataQuery does simply nothing

How to get the selected element of the iOS Voiceover

I have an iOS Swift App and i want to detect which element is selected by the voiceover.
Have tried to send an UIAccessibilitySwitchControlStatusDidChangeNotification but i don't get this to work.
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "handleVoiceoverSelection",
name: UIAccessibilitySwitchControlStatusDidChangeNotification,
object: nil)
...
func handleVoiceoverSelection(){
println("!!!! element selected !!!!")
}
Is there any way?
Maybe check out UIAccessibilityFocus?
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIAccessibilityFocus_Protocol/index.html

Resources