Rewrite of Swift Code 2 into version 3

I am trying to rewrite the following code into Swift 3, can anyone Help?


self.queryDidUpdateObserver = NSNotificationCenter

.defaultCenter()

.addObserverForName(NSMetadataQueryDidUpdateNotification,

object: metadataQuery,

queue: NSOperationQueue.mainQueue()) { (notification) in

self.queryUpdated()

}

self.queryDidFinishGatheringObserver = NSNotificationCenter

.defaultCenter()

.addObserverForName(NSMetadataQueryDidFinishGatheringNotification,

object: metadataQuery,

queue: NSOperationQueue.mainQueue()) { (notification) in

self.queryUpdated()

}

Xcode should help you with at least most of these changes, but here is the code converted for you:


        self.queryDidUpdateObserver = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidUpdate,
                                object: metadataQuery,
                                queue: OperationQueue.main) { (notification) in
                                    self.queryUpdated()
        }
        self.queryDidFinishGatheringObserver = NotificationCenter.default.addObserver(forName: NSNotification.Name.NSMetadataQueryDidFinishGathering,
                                object: metadataQuery,
                                queue: OperationQueue.main) { (notification) in
                                    self.queryUpdated()
        }

Many thanks!

I'am starting to code app's and I've several issue with Swift 3.0. The most tutorial on the web are coded with swift 2.3 and I've have to debug. It's is possible to downgrade to swift 2.3 ?

You can't 'downgrade' however if you have an existing project that is written in swift 2.3 and hasn't been converted you can actually get Xcode to support the use of legacy code.

To achieve this all you need to do is the following:

  • Click on your blue project name in the navigator
  • Click on the app target
  • Click on Build Settings
  • Look for Swift Compiler - Version
  • Look for Use Legacy Swift Language Version
  • Select yes
  • Press Cmd + alt + shift + k to clean the build folder
  • Press Cmd + Shift + B to run a code analysis to make sure that the app will compile with out errors
  • At this point it should run without any sort of error
  • If this is the case just run as normal on either your device or simulator


However I do suggest updating the project settings to make sure that Xcode handles your code properly



I hope this works!

Rewrite of Swift Code 2 into version 3
 
 
Q