“(Class) -> () -> (Class)”类型的值没有成员 'variable'
Value of type '(Class) -> () -> (Class)' has no member 'variable'
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'
所以显然它无法在我的 class ChatLogController
中找到 friend
某种原因,还是我错了?这就是我的猜测,因为它找不到 friend
作为 self
的成员。
这里是 friend
变量,它在 ChatLogController
里面 class:
var friend: Friend?
{
didSet
{
navigationItem.title = friend?.name
messages = friend?.messages?.allObjects as? [Message]
messages = messages?.sorted(by: {[=14=].date!.compare(.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
,这意味着
delaying the creation of an object or some other expensive process
until it’s needed
使 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
}()
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'
所以显然它无法在我的 class ChatLogController
中找到 friend
某种原因,还是我错了?这就是我的猜测,因为它找不到 friend
作为 self
的成员。
这里是 friend
变量,它在 ChatLogController
里面 class:
var friend: Friend?
{
didSet
{
navigationItem.title = friend?.name
messages = friend?.messages?.allObjects as? [Message]
messages = messages?.sorted(by: {[=14=].date!.compare(.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
,这意味着
delaying the creation of an object or some other expensive process until it’s needed
使 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
}()