首页 > 解决方案 > 有人可以帮我解决这个警告:冗余一致性约束'T':'ReusableView'吗?我到处搜索,但没有发现任何帮助

问题描述

有人可以帮我解决这个警告吗?

冗余一致性约束“T”:“ReusableView”

我到处搜索,但没有发现任何帮助。

import UIKit

extension UICollectionView {

    func register<T: UICollectionViewCell>(_: T.Type) where T: ReusableView, T: NibLoadableView {
        //warning: Redundant conformance constraint 'T': 'ReusableView'

        let nib = UINib(nibName: T.nibName, bundle: nil)
        register(nib, forCellWithReuseIdentifier: T.reuseIdentifier)
    }

    func dequeueReusableCell<T: UICollectionViewCell>(forIndexPath indexPath: IndexPath) -> T where T: ReusableView {
        //warning: Redundant conformance constraint 'T': 'ReusableView'

        guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath as IndexPath) as? T else {
            fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)")
        }
        return cell
    }
}

extension UICollectionViewCell: ReusableView {}

标签: iosswift

解决方案


首先,编译器警告约束是多余的。这不是错误。你可以保持原样。

如何修复警告?

您的 UICollectionViewCell 已经在代码中的某处扩展以符合ReusableView协议。

这就是您不必再次应用此约束的原因。

func register<T: UICollectionViewCell>(_: T.Type) where T: NibLoadableView {
}

它对我的应用程序的行为有影响吗?

这对您的应用程序的行为没有影响,因为删除的约束redundant如警告中所述。


推荐阅读