let fetchedResultsController: NSFetchedResultsController = { () -> NSFetchedResultsController<NSFetchRequestResult> in
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Message")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
fetchRequest.predicate = NSPredicate(format: "friend.name = %@", self.friend!.name!)
let delegate = UIApplication.shared.delegate as! AppDelegate
let context = delegate.persistentContainer.viewContext
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
return frc
}()
在显示以下行的位置:
fetchRequest.predicate = NSPredicate(format: "friend.name = %@", self.friend!.name!)
我收到错误:Value of type '(ChatLogController) -> () -> (ChatLogController)' has no member 'friend'
因此,由于某种原因,它显然无法在我的类friend
中找到ChatLogController
,还是我错了?这就是我的猜测,因为它找不到friend
作为self
的成员。这是
friend
变量,它在ChatLogController
类的内部:var friend: Friend?
{
didSet
{
navigationItem.title = friend?.name
messages = friend?.messages?.allObjects as? [Message]
messages = messages?.sorted(by: {$0.date!.compare($1.date! as Date) == .orderedAscending})
}
}
任何人都知道为什么我会收到此错误以及如何解决它?
编辑:我注意到,如果我从第一个代码段的第一行中删除
() -> NSFetchedResultsController<NSFetchRequestResult> in
,该错误消失了,但是反而出现了以下错误:Unable to infer complex closure return type; add explicit type to disambiguate
最佳答案
您已将friend声明为optional
属性,该属性在fetchedResultsController的initialisation
时尚未初始化,该属性是一个常量,因此会产生错误。要解决此问题,您可以使用lazy initialisation
,这意味着
使fetchedResultsController属性变得懒惰,这将延迟对象的创建,直到需要它为止
lazy var fetchedResultsController: NSFetchedResultsController = { [unowned self] () -> NSFetchedResultsController<NSFetchRequestResult> in
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Message")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
fetchRequest.predicate = NSPredicate(format: "friend.name = %@", self.friend!.name!)
let delegate = UIApplication.shared.delegate as! AppDelegate
let context = delegate.persistentContainer.viewContext
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
return frc
}()
关于ios - 类型 '(Class) -> () -> (Class)'的值没有成员 'variable',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50134350/