2015-06-26 4 views
9

Ho appena aggiornato a Xcode 7 beta con Swift 2.0. E quando ho aggiornato il mio progetto a Swift 2.0, ho ricevuto questo errore: "Tipo 'OSType' non è conforme al protocollo 'AnyObject' in Swift 2.0". Il mio progetto funziona perfettamente in Swift 1.2. E qui è il codice ha ricevuto l'errore:Il tipo 'OSType' non è conforme al protocollo 'AnyObject' in Swift 2.0

videoDataOutput = AVCaptureVideoDataOutput() 
     // create a queue to run the capture on 
     var captureQueue=dispatch_queue_create("catpureQueue", nil); 
     videoDataOutput?.setSampleBufferDelegate(self, queue: captureQueue) 

     // configure the pixel format    
     **videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA]** // ERROR here! 

     if captureSession!.canAddOutput(videoDataOutput) { 
      captureSession!.addOutput(videoDataOutput) 
     } 

Ho cercato di convertire kCVPixelFormatType_32BGRA a ANYOBJECT, ma non ha funzionato. Qualcuno potrebbe aiutarmi? Scusa per il mio pessimo inglese! Grazie!

risposta

33

Questa è la definizione kCVPixelFormatType_32BGRA in Swift 1.2:

var kCVPixelFormatType_32BGRA: Int { get } /* 32 bit BGRA */ 

Questa è la sua definizione in Swift 2.0:

var kCVPixelFormatType_32BGRA: OSType { get } /* 32 bit BGRA */ 

In realtà il OSType è un UInt32 che non può implicita convertire in NSNumber:

When you write let ao: AnyObject = Int(1) , it isn’t really putting an Int into an AnyObject. Instead, it’s implicitly converting your Int into an NSNumber, which is a class, and then putting that in.

https://stackoverflow.com/a/28920350/907422

Quindi provare questo:

videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey: Int(kCVPixelFormatType_32BGRA)] 

o

videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA) 
+1

che risolve effettivamente il problema. Ma la tua risposta sarebbe ancora più utile con una * spiegazione * perché è necessario, in particolare dal momento che il codice OP ha funzionato in Swift 1.2. –

+0

Grazie. Per me funziona. – hiennv92

+1

@MartinR Ciao, ho aggiornato la mia risposta ... Spero che possa spiegare in modo più chiaro. – Bannings