2015-06-29 10 views
8

मैं अपने ऐप को एक्सकोड 7 के साथ स्विफ्ट 2 में अपडेट कर रहा हूं। यह मेरा कंट्रोलर व्यूडिडलोड का कोड है।स्विफ्ट 2 कॉल व्यूडिडलोड छवि में अतिरिक्त तर्क त्रुटि

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!) 
} 

लाइन

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

पर मैं कॉल में त्रुटि अतिरिक्त तर्क त्रुटि मिलती है। मैंने विधि से पहले ही कोशिश की है {} और पकड़ें {} लेकिन यह काम नहीं करता है, मुझे हमेशा यह त्रुटि मिलती है।

मैं इसे कैसे ठीक कर सकता हूं? धन्यवाद

+0

स्विफ्ट 2 की नई सुविधाओं में से एक यह है कि यह 'एनएसईआरआरओ' सूचक को स्वीकार करने के बजाय फेंकता है, इसलिए यह संभवतः आपकी समस्या है। – sbarow

+0

@ सबरो अच्छा नोट! – fqdn

+0

@sbarow तो मैं इसे कैसे ठीक करता हूं? – markutus

उत्तर

0

यह है कि प्रकार विधि की तरह नहीं दिखता AVCaptureDeviceInput पर अब मौजूद है, को देखने के ->https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVCaptureDeviceInput_Class/index.html#//apple_ref/swift/cl/c:objc(cs)AVCaptureDeviceInput

(ऐसा लगता है कि आप शायद init(device:) उपयोग करना चाहते हैं)

एक आसान टिप के रूप में

...: जब भी आप वेब के माध्यम से डेवलपर लाइब्रेरी ब्राउज़ नहीं कर रहे हैं, तो यदि आप सुनिश्चित नहीं हैं कि आप दस्तावेज़ीकरण का नवीनतम 'प्रीरलीज' संस्करण देख रहे हैं तो URL-> 'लाइब्रेरी' और '/ ios' के बीच URL /> prerelease 'जोड़ें। 'यदि आवश्यक हो)

+0

तो कोड में मुझे क्या परिवर्तन करने की आवश्यकता है? – markutus

+0

@markutus क्या आप ऊपर से जुड़े प्रारंभकर्ता विधि के लिए नया हस्ताक्षर ढूंढने में सक्षम थे? और क्या आप अपनी समस्या का समाधान करने में सक्षम थे? मुझे बताना, अगर आप को किसी भी अधिक मदद की ज़रूरत हो! मेरे द्वारा लिंक किए गए दस्तावेज़ आपको वहां लाने में सक्षम होना चाहिए – fqdn

16

स्विफ्ट 2 ने नई त्रुटि प्रबंधन शुरू की। समस्या आप कर रहे हैं हल करने के लिए, आप AVCaptureDevice पद्धति की अपेक्षा NSError वस्तु गुजर के बजाय त्रुटि catch की जरूरत है:

: एक अधिक विस्तृत विवरण के लिए

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) 
    } 
} 

इस लेख पर एक नजर है Error Handling in Swift 2.0

संबंधित मुद्दे