【问题标题】:AVCaptureFileOutputRecordingDelegate not recognizing implemented methods Swift 3AVCaptureFileOutputRecordingDelegate 无法识别已实现的方法 Swift 3
【发布时间】:2017-09-13 03:29:27
【问题描述】:

长话短说,我正在实现 AVCaptureFileOutputRecordingDelegate 但 XCode 无法识别所需的方法。

自从我开始将我的代码迁移到 Swift 3 之后,我实现这个委托的类一直给出一个错误提示

Type 'VideoViewController' does not conform to protocol 'AVCaptureFileOutputRecordingDelegate'

我知道我只需要实现这个方法

public func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!)

所以,我到现在为止做了什么:

  • 清理项目(无效)
  • 清理了构建文件夹(无效)
  • 创建了一个新项目来实现这个委托(这个工作)
  • 评论了不属于此委托的所有其他功能(无效)
  • 在我的项目中创建了一个新的 ViewController,它只实现了这个委托(没有工作)
  • 关闭 XCode 以确保这不是缓存问题(不起作用 =/)
  • 试图在旧版本的 XCode 中运行项目

有时 XCode 建议修复它来实现“缺失”方法

    /*!
 @method captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:
 @abstract
 Informs the delegate when all pending data has been written to an output file.

 @param captureOutput
 The capture file output that has finished writing the file.
 @param fileURL
 The file URL of the file that has been written.
 @param connections
 An array of AVCaptureConnection objects attached to the file output that provided the data that was written to the file.
 @param error
 An error describing what caused the file to stop recording, or nil if there was no error.

 @discussion
 This method is called when the file output has finished writing all data to a file whose recording was stopped, either because startRecordingToOutputFileURL:recordingDelegate: or stopRecording were called, or because an error, described by the error parameter, occurred (if no error occurred, the error parameter will be nil). This method will always be called for each recording request, even if no data is successfully written to the file.

 Clients should not assume that this method will be called on a specific thread.

 Delegates are required to implement this method.
 */
@available(iOS 4.0, *)
public func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {

}

但是在这之后,XCode 说方法是重复的,但仍然说该类没有实现所需的方法

有人知道为什么会发生这种情况以及如何解决吗?

谢谢大家

编辑 1

这是我在项目中创建的用于测试的类并发生相同的错误

import Foundation
import AVFoundation

class VVC : UIViewController, AVCaptureFileOutputRecordingDelegate {
/*!
 @method captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:
 @abstract
 Informs the delegate when all pending data has been written to an output file.

 @param captureOutput
 The capture file output that has finished writing the file.
 @param fileURL
 The file URL of the file that has been written.
 @param connections
 An array of AVCaptureConnection objects attached to the file output that provided the data that was written to the file.
 @param error
 An error describing what caused the file to stop recording, or nil if there was no error.

 @discussion
 This method is called when the file output has finished writing all data to a file whose recording was stopped, either because startRecordingToOutputFileURL:recordingDelegate: or stopRecording were called, or because an error, described by the error parameter, occurred (if no error occurred, the error parameter will be nil). This method will always be called for each recording request, even if no data is successfully written to the file.

 Clients should not assume that this method will be called on a specific thread.

 Delegates are required to implement this method.
 */
@available(iOS 4.0, *)
func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {

}
}

【问题讨论】:

  • 显示与分配代理相关的代码。
  • 这是我的班主任'class VideoViewController: PublishViewController, AVCaptureFileOutputRecordingDelegate'
  • 你不需要import UIKit 吗?
  • 不一定,但我添加只是为了确保...继续显示相同的错误
  • 既然您已经完成了清理项目,请尝试清理 XCode Derived Data

标签: ios xcode swift3 migration swift2.3


【解决方案1】:

不确定这是否是您的问题的原因,但我遇到了同样的问题,这是由于 Candidate has non matching type 错误造成的。

我创建了一个名为 Error 的新类型,其名称空间的完整名称是 [Namespace].Error,它与 Swift 的 Error 类型冲突。

在实现AVCaptureFileOutputRecordingDelegate 时,XCode 将添加协议函数签名,并且didFinishRecordingTo 函数具有Error 类型。我不得不将其更改为 Swift 的 Swift.Error 类型版本

func fileOutput(
    _ output: AVCaptureFileOutput,
    didFinishRecordingTo outputFileURL: URL,
    from connections: [AVCaptureConnection],
    error: Swift.Error?
) {
    ...
}

【讨论】:

  • 哦,是的!这个应该被接受!我还必须将 Error 替换为 Swift.Error
  • 有人能接受这个作为正确答案吗?这是一个完美的解决方案!非常感谢!
猜你喜欢
  • 2017-04-29
  • 2018-04-24
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
相关资源
最近更新 更多