我的问题是如何使用需要泛型的方法扩展类型。
我想延长
IndexPath
并添加一个方法来检查它在给定的
NSFetchedResultsController
. 这是我的分机号:
public extension IndexPath {
func isValid(in fetchedResultsController: NSFetchedResultsController<NSManagedObject>) -> Bool {
guard let sections = fetchedResultsController.sections,
section < sections.count,
item < sections[section].numberOfObjects else {
return false
}
return true
}
在我的
UICollectionViewController
班级,我声明
myFetchedResultsController
:
private var myFetchedResultsController: NSFetchedResultsController<MyModel>!
MyModel
是一个
NSManagedObject
.
但是,当我尝试调用新方法时:
indexPath.isValid(in: myFetchedResultsController)
我得到一个错误:
无法转换“nsfetchedResultsController!”类型的值到预期的参数类型“nsfetchedResultsController”。
插入“as!”未保存的结果控制器
我错过了什么?
我的模型
是
被管理对象
为什么要我投呢?