gpt4 book ai didi

ios - 类型 'ParseSource' 不符合协议(protocol) 'InputSource'

转载 作者:行者123 更新时间:2023-11-30 11:19:26 25 4
gpt4 key购买 nike

我能够将此项目更新到最新版本的 POD,但一位老开发人员编写了此代码,我收到错误:

Type 'ParseSource' does not conform to protocol 'InputSource'

就我的一生而言,我明白这里缺少了一些东西,我只是无法完全理解这个错误。

代码片段如下:

public class ParseSource: NSObject, InputSource {
var file: PFFile
var placeholder: UIImage?

/// Initializes a new source with URL and optionally a placeholder
/// - parameter url: a url to be loaded
/// - parameter placeholder: a placeholder used before image is loaded
public init(file: PFFile, placeholder: UIImage? = nil) {
self.file = file
self.placeholder = placeholder
super.init()
}

public func load(to imageView: UIImageView, with callback: @escaping (UIImage) -> Void) {
self.file.getDataInBackground {(data: Data?, error: Error?) in
if let data = data, let image = UIImage(data: data) {
imageView.image = image
callback(image)
}
}
}
}

这是我们正在使用的 ImageSlider 框架的 InputSource 核心文件:

import UIKit

/// A protocol that can be adapted by different Input Source providers
@objc public protocol InputSource {
/**
Load image from the source to image view.
- parameter imageView: Image view to load the image into.
- parameter callback: Callback called after image was set to the image view.
- parameter image: Image that was set to the image view.
*/
func load(to imageView: UIImageView, with callback: @escaping (_ image: UIImage?) -> Void)

/**
Cancel image load on the image view
- parameter imageView: Image view that is loading the image
*/
@objc optional func cancelLoad(on imageView: UIImageView)
}

/// Input Source to load plain UIImage
@objcMembers
open class ImageSource: NSObject, InputSource {
var image: UIImage!

/// Initializes a new Image Source with UIImage
/// - parameter image: Image to be loaded
public init(image: UIImage) {
self.image = image
}

/// Initializes a new Image Source with an image name from the main bundle
/// - parameter imageString: name of the file in the application's main bundle
public init?(imageString: String) {
if let image = UIImage(named: imageString) {
self.image = image
super.init()
} else {
return nil
}
}

public func load(to imageView: UIImageView, with callback: @escaping (UIImage?) -> Void) {
imageView.image = image
callback(image)
}
}

我想知道的是如何解决上述错误以及到底是什么原因导致此错误,因此如果我再次遇到此错误,我知道解决它的最佳方法。

最佳答案

看起来您的签名在 ParseSource 的实现上略有偏差。注意协议(protocol)上的回调签名:

带有回调:@escaping (_ image: UIImage?) -> Void

您的实现类型不匹配,并且不采用可选图像:

带回调:@escaping (UIImage) -> Void

更改您的实现以匹配定义应该可以修复它(在 Playground 中复制)。

关于ios - 类型 'ParseSource' 不符合协议(protocol) 'InputSource',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51446633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com