SWIFTUI 警告 imagePickerController 几乎匹配 - 仅在现有的 swift 项目文件中发生

Posted

技术标签:

【中文标题】SWIFTUI 警告 imagePickerController 几乎匹配 - 仅在现有的 swift 项目文件中发生【英文标题】:SWIFTUI warning imagePickerController nearly matches - happens only in existing swift project file 【发布时间】:2020-10-09 06:17:19 【问题描述】:

我正在使用一些快速视图升级现有应用程序。现在我收到警告:

实例方法'imagePickerController(:didFinishPickingMediaWithInfo:)' 几乎匹配协议'UIImagePickerControllerDelegate'的可选要求'imagePickerController(:didFinishPickingMediaWithInfo:)'

函数:

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) 
        if let image = info[.originalImage] as? UIImage 
            self.onImagePicked(image)
        
       
        self.onDismiss()
    

另外我收到错误消息:

无法根据成员“originalImage”推断上下文基础

有趣的是,如果我将代码复制到一个新项目中,一切都很好(没有警告,没有错误)。为了检查我是否可能受到现有项目中其他视图/方法的影响,我复制了现有项目并删除了所有其他文件,除了带有图像选择器的文件 - 仍然是警告和错误。

是否有任何设置或其他问题可能是原因?任何帮助都非常感谢 - 现在正在第三天工作....

整个代码是:

import SwiftUI

public struct ImagePickerView: UIViewControllerRepresentable 

    private let sourceType: UIImagePickerController.SourceType
    private let onImagePicked: (UIImage) -> Void
    @Environment(\.presentationMode) private var presentationMode

    public init(sourceType: UIImagePickerController.SourceType, onImagePicked: @escaping (UIImage) -> Void) 
        self.sourceType = sourceType
        self.onImagePicked = onImagePicked
    

    public func makeUIViewController(context: Context) -> UIImagePickerController 
        let picker = UIImagePickerController()
        picker.sourceType = self.sourceType
        picker.delegate = context.coordinator
        return picker
    

    public func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) 

    public func makeCoordinator() -> Coordinator 
        Coordinator(
            onDismiss:  self.presentationMode.wrappedValue.dismiss() ,
            onImagePicked: self.onImagePicked
        )
    

      public class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate 

        private let onDismiss: () -> Void
        private let onImagePicked: (UIImage) -> Void

        init(onDismiss: @escaping () -> Void, onImagePicked: @escaping (UIImage) -> Void) 
            self.onDismiss = onDismiss
            self.onImagePicked = onImagePicked
        
        
         public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) 
            if let image = info[.originalImage] as? UIImage 
                self.onImagePicked(image)
            
            print("vor dismiss")
            self.onDismiss()
        
        
        public func imagePickerControllerDidCancel(_: UIImagePickerController) 
            self.onDismiss()
        

    


项目格式兼容 Xcode 12.0,ios Deployment Target 为 14.0, 该项目包括原来还观看

【问题讨论】:

【参考方案1】:

得到答案,无法解释,只能代替

 public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) 

它适用于

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 

不过,如果有人能解释一下,问题就解决了 - 非常欢迎

【讨论】:

以上是关于SWIFTUI 警告 imagePickerController 几乎匹配 - 仅在现有的 swift 项目文件中发生的主要内容,如果未能解决你的问题,请参考以下文章

iOS swiftUI 应用警告 RSABSSATokenBlinder 在两个位置实现

SwiftUI 在一种情况下而不是另一种情况下查看层次结构警告

基本的 SwiftUI 选择器在屏幕变化时发出严重警告和奇怪的动作

Xcode 13.2.1生成SwiftUI+CoreData项目运行时提示:persistent container with no stores loaded 警告的解决

SwiftUI 显示/关闭键盘

为啥我的 SwiftUI 动画在旋转时恢复到前一帧?