2015-06-29 16 views
8

Uygulamamı Xcode 7 ile Swift 2'ye güncelliyorum. Bu ViewController viewDidLoad kodum. Swift 2 Call viewdidload resminde ek argüman hatası

let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error) 

hattı

üzerinde

override func viewDidLoad() { 
     super.viewDidLoad() 

    // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video 
    // as the media type parameter. 
    let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 

    // Get an instance of the AVCaptureDeviceInput class using the previous device object. 
    var error:NSError? 

    let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error) 

    if (error != nil) { 
     // If any error occurs, simply log the description of it and don't continue any more. 
     print("\(error?.localizedDescription)") 
     return 
    } 

    // Initialize the captureSession object. 
    captureSession = AVCaptureSession() 
    // Set the input device on the capture session. 
    captureSession?.addInput(input as! AVCaptureInput) 

    // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session. 
    let captureMetadataOutput = AVCaptureMetadataOutput() 
    captureSession?.addOutput(captureMetadataOutput) 

    // Set delegate and use the default dispatch queue to execute the call back 
    captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue()) 
    captureMetadataOutput.metadataObjectTypes = supportedBarCodes 

    // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer. 
    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
    videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
    videoPreviewLayer?.frame = view.layer.bounds 
    view.layer.addSublayer(videoPreviewLayer!) 

    // Start video capture. 
    captureSession?.startRunning() 

    // Move the message label to the top view 
    view.bringSubviewToFront(messageLabel) 

    // Initialize QR Code Frame to highlight the QR code 
    qrCodeFrameView = UIView() 
    qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor 
    qrCodeFrameView?.layer.borderWidth = 2 
    view.addSubview(qrCodeFrameView!) 
    view.bringSubviewToFront(qrCodeFrameView!) 
} 

i çağrısında hata Ekstra argüman hatası alıyorum. Ben zaten yöntemle çalıştım {} ve yakala {} ama işe yaramadı, her zaman bu hatayı alıyorum.

Bunu nasıl düzeltebilirim? Teşekkür

+0

Swift 2'nin yeni özelliklerinden biri, bir 'NSError' işaretçisini kabul etmek yerine onu atar, bu nedenle büyük olasılıkla sorununuz olur. – sbarow

+0

@sbarow iyi not! – fqdn

+0

@sbarow Peki bunu nasıl düzelttim? – markutus

cevap

0
bu tip yöntemine benzemiyor

AVCaptureDeviceInput üzerine artık var, bakınız ->https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVCaptureDeviceInput_Class/index.html#//apple_ref/swift/cl/c:objc(cs)AVCaptureDeviceInput

(o muhtemelen init(device:) kullanmak istediğiniz gibi görünüyor)

kullanışlı ipucu olarak

...: Web üzerinden geliştirici kitaplığına göz attığınızda, dokümanların en son 'yayın öncesi' sürümünü görüp görmediğinizden emin değilseniz, 'kitaplık' ve '/ ios arasında URL'yi> -' ekle '/ önseçim' seçeneğini işaretleyin. 'Gerekirse'

+0

kodda yapmam gereken değişiklikler nedir? – markutus

+0

@markutus Yukarıda bağladığım başlatıcı yönteminin yeni imzasını bulabildiniz mi? ve sorununuzu çözebildiniz mi? Eğer daha fazla yardıma ihtiyacınız varsa bana bildirin! Bağladığım belgeler sizi oraya götürebilirdi – fqdn

16

Swift 2 yeni hata işleme yöntemini tanıttı.

: Bu makalede bir göz daha derinlemesine açıklaması için

override func viewDidLoad() { 
    super.viewDidLoad() 

    do { 
     let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 
     let input = try AVCaptureDeviceInput(device: captureDevice) 
     // Do the rest of your work... 
    } catch let error as NSError { 
     // Handle any errors 
     print(error) 
    } 
} 

: yaşadığınız sorunu çözmek için, AVCaptureDevice yöntemine NSError nesneyi geçirmeden yerine hatayı catch gerekir Error Handling in Swift 2.0