I want to be able to use a variable in one file in another.
For example I created an array of core data items in one view controller, but I want to use it in another view controller.
I want to be able to use a variable in one file in another.
For example I created an array of core data items in one view controller, but I want to use it in another view controller.
Core Data is to manage the model layer objects in your application. Why did you create an array of core data items in one view controller?
According to Core Data Programming Guide, you should do do initializing the Core Data Stack.
just like
class MyDataController: NSObject {
var persistentContainer: NSPersistentContainer!
// ...
}
And you can fetch it in almost every file. just like :
let moc = …
let employeesFetch = NSFetchRequest(entityName: "Employee")
do {
let fetchedEmployees = try moc.executeFetchRequest(employeesFetch) as! [EmployeeMO]
} catch {
fatalError("Failed to fetch employees: \(error)")
}
You could create one property in ViewControllerB like this,
class ViewControllerB: UIViewController {
var dataItems: [DataItem] = []
}
so when you navigate from ViewControllerA to ViewControllerB you could set the data to the property like below,
let viewControllerB = ViewControllerB()
viewControllerB.dataItems = [...]
navigationController?.pushViewController(viewControllerB, animated: true)
Hope this helps!