In Swift, I have a computed property called managedObjectContext: NSManagedObjectContext
. when I try to initialise it like:
var managedObjectContext: NSManagedObjectContext? {
get {
createManagedObjectContext()
return self.managedObjectContext
}
set(newManagedObjectContext) {
self.manageObjectContext = newManagedObjectContext
}
}
func createManagedObjectContext() -> NSManagedObjectContext {
if let coordinator = persistentStorageCoordinator() as NSPersistentStoreCoordinator!
{
self.managedObjectContext = NSManagedObjectContext()
self.managedObjectContext!.persistentStoreCoordinator = coordinator
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleBackgroundMOCDidSaveNotification:", name: NSManagedObjectContextDidSaveNotification, object: self.managedObjectContext)
}
return self.managedObjectContext!;
}
I am getting an error saying : Cannot assign to "managedObjectContext" in self
.
Can anybody help to fix this?
Thanks in advance.
You also receive this compile error when you're trying to assign a value to a let
constant. Changing the variable's type to var
should solve it, in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With