-4

In Xcode 8.0, I'm getting an error while compiling for Swift 3.0

Error: "'NSOperationQueue' has been renamed to OperationQueue"

When I switch to OperationQueue.mainQueue() the method "mainQueue" doesn't exist! What's replaced it???

    let manager = CMMotionManager()
    if manager.isGyroAvailable {
        manager.gyroUpdateInterval = 0.1
        manager.startGyroUpdates()
        let queue = NSOperationQueue.mainQueue()
        manager.startGyroUpdatesToQueue(queue) {
            (data, error) in
            // ...
        }
    }
    if manager.isAccelerometerAvailable {
        manager.accelerometerUpdateInterval = 0.01
        manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
            [weak self] (data: CMAccelerometerData?, error: NSError?) in
            if let acceleration = data?.acceleration {
                self.physicsWorld.gravity = CGVectorMake(acceleration.x, acceleration.y)
            }
        }
    }

How to I fix the above code so NSOperationQueues are formatted correctly?

Jono Tho'ra
  • 1,476
  • 3
  • 18
  • 28
  • The error is pretty clear. Change `NSOperationQueue` to `OperationQueue`. See the docs for `OperationQueue` for its API. – rmaddy Oct 17 '16 at 04:28
  • 2
    Which part of "NSOperationQueue has been renamed to OperationQueue" do you not understand? – matt Oct 17 '16 at 04:30
  • That's nice; I'm specifically noting my specific situation where the xcode error that has the 'fix-it' option in line. When using the fix-it ability for the error, it merely renames NSOperationQueue to OperationQueue. The result isn't very effective and doesn't really 'fix' anything. After the rename, it doesn't really give good feedback on how else to remedy the issue... Plus, 'would be nice to have a quick reference on stackoverflow so anyone else encountering such a problem can find a quick resolution instead of having to search into documentation. – Jono Tho'ra Oct 17 '16 at 04:36

1 Answers1

1

Write OperationQueue.main.

Fairly simple. Actually, in your code, you can skip a step and just write .main where an operation queue is expected. So:

manager.startGyroUpdatesToQueue(.main) {
matt
  • 515,959
  • 87
  • 875
  • 1,141