简体   繁体   中英

Error: Type 'String!' has no member 'video'

import UIKit
import AVKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

//start camera

        let captureSession = AVCaptureSession()

        guard let captureDevice =
           AVCaptureDevice.default(for: .video) else { return }
        guard let input = try? AVCaptureDeviceInput(device:
                captureDevice ) else { return }
        captureSession.addInput(input)

        captureSession.startRunning()
    }
}

I get the error in this line at (for: .video ):

AVCaptureDevice.default(for: .video) else { return }

用AVMediaTypeVideo替换AVMediaType.video对我有用。

The method

class func `default`(_ deviceType: AVCaptureDevice.DeviceType, 
             for mediaType: AVMediaType?, 
        position: AVCaptureDevice.Position) -> AVCaptureDevice?

Accepts a DeviceType, which can be:

AVCaptureDeviceTypeBuiltInMicrophone
AVCaptureDeviceTypeBuiltInWideAngleCamera
AVCaptureDeviceTypeBuiltInTelephotoCamera
AVCaptureDeviceTypeBuiltInDualCamera
AVCaptureDeviceTypeBuiltInDuoCamera

I think I have figured it out. The documentation I was looking at was for Xcode 9. What you need is first to import AVFoundation and then use this function:

class func defaultDevice(withDeviceType deviceType: AVCaptureDeviceType!, 
                                         mediaType: String!, 
                                          position: AVCaptureDevicePosition) -> AVCaptureDevice!

https://developer.apple.com/documentation/avfoundation/avcapturedevice/2361508-default?changes=latest_minor

For example:

let c = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInTelephotCamer,
                                           mediaType: AVMediaTypeVideo,
                                            position: AVCaptureDevicePosition.front)

I'm just trying to convert my project to Swift4. This works for me.

device = AVCaptureDevice.default(for: AVMediaType.video)

I got this exact error when building with "Swift language Version" set to "Swift 3.3".

Changing the swift language version from "3.3" to "4.1" solved the issue for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM