将fetchedresultscontroller var转换为swift 3时遇到问题

时间:2016-09-17 13:28:23

标签: ios swift swift3

我试图将其转换为swift 3

lazy var fetchedResultsControler: NSFetchedResultsController = { 
    let fetchRequest = NSFetchRequest(entityName: "Mesages")
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)]
    fetchRequest.predicate = NSPredicate(format: "user.id = %@", self.friend!.id!)
    let moc = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext
    let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
    frc.delegate = self
    return frc
}()

但它将其转换为:

lazy var fetchedResultsControler: NSFetchedResultsController = { () -> <<error type>> in 
    let fetchRequest = NSFetchRequest(entityName: "Mesages")
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)]
    fetchRequest.predicate = NSPredicate(format: "user.id = %@", self.friend!.id!)
    let moc = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext
    let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
    frc.delegate = self
    return frc
}()

并且它给我带来了将冒号放在随机位置的错误,你如何在swift 3中声明这样的东西

1 个答案:

答案 0 :(得分:1)

您需要指定ResultType

lazy var fetchedResultsControler: NSFetchedResultsController<NSFetchRequestResult> = {
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Mesages")
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)]
    fetchRequest.predicate = NSPredicate(format: "user.id = %@", self.friend!.id!)
    let moc = (UIApplication.shared.delegate as! AppDelegate).managedObjectContext
    let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
    frc.delegate = self
    return frc
}()