我将View对象从ViewcontrollerA传递给ViewcontrollerB。但是输出是不同的。我无法弄清楚ViewcontrollerB中实体为空的原因。
我曾经在下面的代码中打印两个控制器中的Article对象。
print(String(describing:listOfArticles))
输出
ViewcontrollerA。
<Article: 0x600000281810> (entity: Article; id: 0xd00000000168001c <x-coredata://949275DD-E091-49E8-A0D0-2C362B616F97/Article/p90> ; data: <fault>)
ViewcontrollerB。
Optional(<Article: 0x600000283610> (entity: <null>; id: 0xd0000000000c0012 <x-coredata://949275DD-E091-49E8-A0D0-2C362B616F97/Article/p3> ; data: <fault>))
在ViewcontrollerB中,entity: <null>;
始终为null。当我尝试使用该对象时它会崩溃。
推送ViewcontollerA代码: -
guard let viewArticelArray = topStories_presenter.fetchAllAritcleForTopStories(fetchedResultsController:fetchedResultsController) else {
return
}
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "NewsCollectionViewController") as! NewsCollectionViewController
vc.listArticle = viewArticelArray[0]
parentNavigationController!.pushViewController(vc, animated: true)
ViewcontollerB
var listArticle :Article!
override func viewDidLoad() {
super.viewDidLoad()
print(String(describing:listArticle))
}
FetchResultViewController代码:
fileprivate lazy var fetchedResultsController: NSFetchedResultsController<TopStorie> = {
// Create Fetch Request
let moc = CoreDataHelper().managedObjectContext
let fetchRequest: NSFetchRequest<TopStorie> = TopStorie.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key:"typeid", ascending: true)]
fetchRequest.predicate = NSPredicate(format: "category contains[c]%@", self.title!)
// Create Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext:moc, sectionNameKeyPath:"typeid", cacheName: nil)
// Configure Fetched Results Controller
fetchedResultsController.delegate = self
return fetchedResultsController
}()